Doctype

Also known as: DOCTYPE declaration

On this page 7

A doctype, or DOCTYPE declaration, is the short instruction at the very beginning of an HTML document that identifies the document type and selects the browser rendering mode. In modern HTML the declaration is simply <!DOCTYPE html>, and its practical job is to make browsers use standards mode instead of the legacy quirks mode that imitates the behavior of very old browsers.

The doctype is not an HTML element and does not appear in the rendered page. It is a parser directive that becomes a DocumentType node in the DOM, and although it is only fifteen characters long, omitting it can change how layout, tables and CSS behave across the whole page.

How the doctype controls rendering mode

When a browser starts parsing an HTML document, it inspects the doctype token and uses it to choose one of three rendering modes defined by the HTML parsing algorithm. The choice is made once, before any content is laid out, and it applies to the entire document. Changing the doctype later through script has no effect on the mode.

ModeTriggered byBehaviorValue of document.compatMode
No-quirks modeThe HTML5 doctype or strict legacy doctypesFollows current CSS and HTML specificationsCSS1Compat
Limited-quirks modeSome transitional and frameset doctypes with a system identifierStandards behavior except inline image sizing in table cellsCSS1Compat
Quirks modeMissing doctype or old, malformed or partial doctypesEmulates legacy browser bugs for compatibilityBackCompat
Rendering modes selected by the doctype

Quirks mode exists because early web pages were written for browsers whose behavior contradicted the CSS specifications. When standards-compliant engines arrived, browser makers kept the old behavior for documents without a modern doctype, a technique known as doctype switching. Every page that wants predictable modern behavior must therefore opt in with a correct declaration.

Doctype versions from SGML to HTML5

Earlier HTML versions were defined as applications of SGML or XML, so their doctypes referenced a public identifier and a URL to a formal document type definition, the DTD. Browsers never actually downloaded or validated against those DTDs; they only matched the strings to pick a rendering mode.

Doctype familyEraTypical modeStatus today
HTML 4.01 StrictLate 1990sNo-quirksObsolete but still recognized
HTML 4.01 TransitionalLate 1990sLimited-quirks with system identifier, quirks without itObsolete
XHTML 1.0 StrictEarly 2000sNo-quirksObsolete
XHTML 1.0 TransitionalEarly 2000sLimited-quirksObsolete
HTML5 doctypeFrom 2008 onwardNo-quirksCurrent and recommended
Historical doctype families

The HTML5 doctype was designed as the shortest string that reliably triggers no-quirks mode in every browser. It references no DTD and carries no version number, because the HTML Living Standard is versionless and HTML is no longer defined through SGML.

Syntax rules and correct placement

The doctype must be the first thing in the document, before the html element. Only whitespace, comments and a byte order mark may precede it, and placing comments before it is best avoided because some older engines then fell into quirks mode.

  • Case-insensitive keyword: <!DOCTYPE html>, <!doctype html> and mixed case are all valid; uppercase is conventional, lowercase is common in minified output.
  • Exactly one per document: a second doctype later in the file is a parse error and is ignored.
  • No closing tag: the declaration is complete on its own and has no end form.
  • Inside iframes: every framed document selects its own mode, so each needs its own doctype.
  • Served as XHTML: documents delivered as application/xhtml+xml are parsed as XML and always use standards behavior, regardless of doctype.

Developers can verify the active mode by reading document.compatMode in the browser console, where CSS1Compat indicates standards or limited-quirks mode and BackCompat indicates quirks mode. Browser developer tools and HTML validators also report missing or incorrect doctypes.

What changes in quirks mode

Quirks mode alters a specific, documented list of behaviors rather than switching off CSS. The differences are small individually, but together they produce layouts that break in subtle ways, often only in certain components.

QuirkEffect in quirks modeTypical symptom
Unitless lengthsValues such as width: 300 are accepted as pixelsInvalid CSS appears to work, then breaks elsewhere
Percentage heightsBody and html behave differently with height: 100%Full-height layouts collapse or overflow
Table font inheritanceTables do not inherit some font propertiesText in tables uses unexpected sizes
Inline images in cellsNo baseline gap below imagesSpacing changes when the doctype is added
Class and ID matchingSelectors match case-insensitivelyStyles apply inconsistently after fixing the doctype
Selected quirks and their effects

The historical box model quirk, in which old browsers included padding and border inside the declared width, is the best-known example. Modern quirks mode handles most box sizing according to standards, and developers who want the alternative model now use box-sizing: border-box deliberately in standards mode.

Common mistakes and how to avoid them

Doctype problems usually come from templates, generated output and legacy systems rather than from hand-written pages. A server-side include, an output buffer or a stray character before the declaration can silently push a whole site into quirks mode.

  • Missing doctype in email or CMS templates, producing inconsistent rendering between pages.
  • Whitespace or text emitted before the doctype by server scripts, plugins or debug output.
  • Keeping legacy transitional doctypes that select limited-quirks mode and alter table image spacing.
  • Assuming the doctype validates markup: it selects a mode, but it does not enforce correct HTML.

The safe rule is simple: start every HTML document with the HTML5 doctype, followed by an html element with a lang attribute and a meta charset declaration early in the head. This combination gives predictable parsing, rendering and accessibility from the first byte.

Understanding it simply

A doctype is a short label at the top of a web page that tells the browser which rules to follow. Without it, the browser assumes the page might be very old and switches to a compatibility mode that copies the mistakes of browsers from decades ago.

It is like telling a translator which edition of a dictionary to use. With the right label, everything is read the modern way. Without it, words may be interpreted as they were long ago. The takeaway is that one line at the top keeps the whole page behaving predictably.

Applied in practice

A developer maintaining a company intranet notices that a new dashboard layout works in isolation but breaks inside the main portal: full-height sidebars collapse, and some table text appears at the wrong size. Checking document.compatMode in the console shows BackCompat, meaning the portal pages render in quirks mode.

The cause is a legacy header include that prints a blank line and an HTML comment before the template’s doctype, while an older template omits the doctype entirely. The developer removes the stray output, standardizes every template on the HTML5 doctype and retests. The console now reports CSS1Compat, the sidebars fill the viewport and table styles match the design. Two old pages that had relied on unitless CSS lengths are corrected at the same time, and the whole portal now behaves consistently across browsers.