The HTML5 Document: Doctype, Head, and Metadata

The HTML5 document head explained: doctype, lang, charset, viewport, title, description, canonical, icons, social tags and resource hints, in order.

Executive Summary: Every HTML page starts with the same few lines, and most of them are copied without being read. Each one has a job: the doctype keeps the browser out of quirks mode, lang tells screen readers and translators what language they are reading, the charset declaration must arrive in the first 1024 bytes, the viewport tag makes mobile layout work, and the title, description, canonical and social tags decide how the page looks everywhere it is shared. Order matters for a few of them, and a handful of popular tags do nothing at all.
Browser window showing an HTML5 document with doctype, charset and viewport meta tags

Open the source of ten random websites and you will find heads with forty tags in them: three different favicon formats, a keywords meta tag, an X-UA-Compatible line for Internet Explorer, and a viewport tag that disables zoom. Most of it was copied from a boilerplate years ago. Some of it actively hurts.

The HTML document head is the <head> element: metadata about the page that is not displayed as content. Together with the doctype and the root <html> element, it forms the skeleton every page needs before the first heading appears. This guide walks through that skeleton line by line, in the order the lines should appear, and explains what each one actually does. If you want a broader tour of what HTML5 added, I covered that in new and useful features of HTML5.

What is the minimal valid HTML5 document?

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Pricing - Acme Analytics</title>
</head>
<body>
  ...
</body>
</html>

Those six lines are the floor. Everything else is an addition you choose deliberately.

What does the HTML5 doctype do?

<!doctype html> looks like a declaration of HTML version 5, but it is really a mode switch. Browsers have three rendering modes. Without a doctype, they render in quirks mode, emulating the bugs of late-1990s browsers so that very old pages keep working. In quirks mode, width includes padding and border on some elements, table cells do not inherit font sizes, and percentage heights behave strangely.

The short HTML5 doctype puts every browser into standards mode. It is case-insensitive, it must be the very first thing in the file, and nothing else is needed. The long HTML 4 and XHTML doctypes with DTD URLs still work, but they add nothing. To check which mode a page is in, run document.compatMode in the console: "CSS1Compat" means standards mode, "BackCompat" means quirks.

One trap: a byte-order mark or a stray comment before the doctype is fine, but server-side code that echoes whitespace or a debug line before it can push some browsers into quirks mode. If your layout breaks only in production, check the first bytes of the response.

The html element and the lang attribute

<html lang="en"> declares the primary language of the page. It is required for WCAG 2.1 success criterion 3.1.1, Language of Page, and it does real work:

  • Screen readers choose pronunciation rules from it. An English page without lang, read by a screen reader set to French, is read with French pronunciation.
  • Browsers use it to offer translation and to pick hyphenation rules for hyphens: auto.
  • Fonts can render locale-specific glyphs, which matters for Chinese, Japanese and Korean text sharing code points.

Use a BCP 47 tag: en, en-IN, hi, ar. For right-to-left languages add dir="rtl" on the same element. If a passage inside the page is in another language, mark it: <span lang="fr">déjà vu</span>.

In what order should the head be written?

1. Character encoding, first

<meta charset="utf-8">

The HTML specification requires the charset declaration to appear within the first 1024 bytes of the document, because the browser may start decoding before it reaches the rest. Put it first in the head. If it comes after a long inline script or a title containing non-ASCII characters, the browser may have already guessed an encoding and then has to reparse, or worse, shows garbled text. Always use UTF-8; it is the only encoding the specification allows for new documents. Sending Content-Type: text/html; charset=utf-8 as an HTTP header is even better, because headers arrive before any bytes of the body.

2. The viewport

<meta name="viewport" content="width=device-width, initial-scale=1">

Without it, mobile browsers render the page as if the screen were about 980 pixels wide and then shrink it, so text is tiny and media queries never fire. With width=device-width, the layout viewport matches the device width in CSS pixels.

Do not add maximum-scale=1 or user-scalable=no. They stop people with low vision from zooming, which fails WCAG 1.4.4, Resize Text. iOS Safari has ignored them since version 10 for that reason, but other browsers still obey them.

3. The title

The <title> appears in the browser tab, in bookmarks, in screen reader announcements when the page loads, and as the headline in search results. Write it as specific page, then site: “Pricing – Acme Analytics”, not “Acme Analytics | Home | Pricing”. Each page needs a unique title. Search engines usually display around 50 to 60 characters before truncating, so lead with the words that matter.

4. Resource hints and critical CSS

<link rel="preconnect" href="https://fonts.example-cdn.com" crossorigin>
<link rel="preload" href="/fonts/inter-var.woff2" as="font" type="font/woff2" crossorigin>
<link rel="stylesheet" href="/css/main.css">

Stylesheets in the head block rendering until they load, which is what you want for the CSS that styles the first screen. preconnect opens a connection to another origin early, and preload fetches a file the browser would otherwise discover late, such as a font referenced inside CSS. Use both sparingly: preloading ten files means none of them is a priority.

5. Scripts, deferred

<script src="/js/app.js" defer></script>
<script type="module" src="/js/widgets.js"></script>

A plain <script src> in the head stops HTML parsing while it downloads and runs. defer downloads in parallel and runs after parsing, in order. Module scripts are deferred by default. async runs as soon as the file arrives, in any order, which suits independent scripts like analytics. With defer, there is no longer a reason to put scripts at the bottom of the body.

6. Description and canonical URL

<meta name="description" content="Compare Acme Analytics plans: free for 3 projects, Team at $29 per seat per month, and Enterprise with SSO.">
<link rel="canonical" href="https://acme.example/pricing">

Search engines often use the meta description as the snippet under your title, though they may rewrite it. Write it for a person deciding whether to click. The canonical link tells search engines which URL is the main version when the same content is reachable at several, such as with tracking parameters, with and without a trailing slash, or on http and https. Use an absolute URL.

7. Icons

<link rel="icon" href="/favicon.ico" sizes="32x32">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="manifest" href="/site.webmanifest">

That set covers current browsers: an ICO for legacy and tools that request /favicon.ico directly, an SVG that stays sharp and can even switch colors with a prefers-color-scheme media query inside it, a 180×180 PNG for iOS home screens, and a web app manifest for Android with 192 and 512 pixel icons. The long lists of a dozen PNG sizes that favicon generators produce are mostly unnecessary.

8. Social sharing

<meta property="og:type" content="website">
<meta property="og:title" content="Acme Analytics pricing">
<meta property="og:description" content="Free for 3 projects. Team plans from $29 per seat.">
<meta property="og:image" content="https://acme.example/og/pricing.png">
<meta property="og:url" content="https://acme.example/pricing">
<meta name="twitter:card" content="summary_large_image">

Open Graph tags control the preview card on LinkedIn, Facebook, WhatsApp, Slack and most chat apps. Twitter reads Open Graph too and only needs twitter:card to choose the large image layout. Use an absolute image URL, and an image of 1200×630 pixels works well across platforms. These are the tags people notice when they are wrong, because a shared link shows the wrong image or no preview at all.

9. Theme and color scheme

<meta name="color-scheme" content="light dark">
<meta name="theme-color" content="#ffffff" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#0f172a" media="(prefers-color-scheme: dark)">

color-scheme tells the browser your page supports dark mode, so it can render default form controls, scrollbars and the initial background in the right scheme before your CSS loads. That avoids a white flash on dark-mode devices. theme-color tints the browser interface on mobile.

Which head tags can you delete?

Tag Why it can go
<meta name="keywords"> Google has ignored it for ranking since 2009
<meta http-equiv="X-UA-Compatible" content="IE=edge"> Only affected Internet Explorer, which Microsoft retired in June 2022
type="text/javascript" and type="text/css" They are the defaults
<meta name="author">, generator, revisit-after No browser or major search engine uses them
Self-closing slashes, <meta ... /> Allowed but meaningless in HTML; keep or drop for consistency
Twelve favicon sizes The four-line icon set above covers current platforms

A complete head you can copy

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Pricing - Acme Analytics</title>

  <link rel="preload" href="/fonts/inter-var.woff2" as="font" type="font/woff2" crossorigin>
  <link rel="stylesheet" href="/css/main.css">
  <script src="/js/app.js" defer></script>

  <meta name="description" content="Compare Acme Analytics plans and pricing.">
  <link rel="canonical" href="https://acme.example/pricing">

  <link rel="icon" href="/favicon.ico" sizes="32x32">
  <link rel="icon" href="/icon.svg" type="image/svg+xml">
  <link rel="apple-touch-icon" href="/apple-touch-icon.png">
  <link rel="manifest" href="/site.webmanifest">

  <meta property="og:title" content="Acme Analytics pricing">
  <meta property="og:description" content="Compare plans and pricing.">
  <meta property="og:image" content="https://acme.example/og/pricing.png">
  <meta property="og:url" content="https://acme.example/pricing">
  <meta name="twitter:card" content="summary_large_image">

  <meta name="color-scheme" content="light dark">
</head>

Validate the result with the W3C Nu HTML Checker and preview social cards with LinkedIn’s Post Inspector or Facebook’s Sharing Debugger before launch. Both catch mistakes that are invisible in the browser.

Frequently Asked Questions

Is the doctype still required in HTML5?

Yes. Without a doctype, browsers render the page in quirks mode, which emulates old browser bugs in layout and sizing. The short <!doctype html> puts every browser into standards mode and must be the first thing in the document.

Why must the charset meta tag come first?

The HTML specification requires the encoding declaration within the first 1024 bytes, because browsers may start decoding the page before reading further. If it appears late, the browser may guess the wrong encoding and show garbled characters or reparse the page.

Does the meta keywords tag help SEO?

No. Google stopped using the keywords meta tag for ranking in 2009, and other major search engines give it no meaningful weight. The title, meta description and canonical link are the head tags that matter for search.

Should I disable zoom in the viewport meta tag?

No. maximum-scale=1 and user-scalable=no prevent people with low vision from zooming, which fails WCAG’s resize text requirement. Use width=device-width, initial-scale=1 and fix any layout that breaks when zoomed instead.

Before the first heading

The head is invisible, which is why it collects junk. Read yours once, line by line, and ask what each tag does. Keep the charset first, the viewport zoomable, the title specific, the scripts deferred and the social tags absolute. Everything the reader sees later depends on these few lines being right. For the full reference, see MDN glossary entry on the doctype and the head element section of the HTML Standard. Related reading on this site: semantic HTML explained and native lazy loading.

Originally posted on 10 January 2023 and post last modified on 10 January 2023

Doctype Front-End Development HTML5 Meta Tags Metadata SEO

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *