Glossary · Beginner

HTML doctype

The doctype is the first line of an HTML document, and its only job is to keep the browser in standards mode.

Published
Last checked

Definition

The doctype is the declaration <!doctype html> at the very top of an HTML document, before the html element. It is not a tag and not an element. It is a required preamble with a single modern purpose.

Standards mode and quirks mode

Browsers keep a legacy rendering mode for documents written before standards settled. The doctype is what tells a browser not to use it. With the doctype present, the page renders in standards mode. Without it, the browser falls back to quirks mode, where the box model and several other behaviours change to match decades-old bugs.

Why the short form is enough

Older doctypes carried long public identifiers referring to specific document type definitions. HTML is no longer versioned that way. <!doctype html> is the complete modern declaration, and nothing longer adds anything.

It must come first

Anything before the doctype, including a stray comment, a blank line with a byte order mark, or output accidentally emitted by a server template, can trigger quirks mode. This is a common cause of layout differing between a local file and a deployed response.

Common failures

  • A template emitting whitespace or a header before the doctype.
  • Copied legacy doctypes carrying obsolete identifiers.
  • Documents assembled from fragments where the preamble is lost.
  • Quirks mode diagnosed as a CSS bug and worked around instead of fixed.
  • Case or spelling errors that stop it being recognised.

Review question

Open developer tools on the deployed page and check the rendering mode. If it reports quirks mode, look at the first bytes of the response rather than at your stylesheet.