Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท Characters and Codes

HTML Doctypes

The HTML doctype and legacy HTML 4.01 and XHTML doctypes, and how each one puts browsers in standards, limited-quirks or quirks mode.

The doctype is the line at the very top of an HTML file. Today it has only one job: to make the browser render the page in standards mode. Without it, browsers fall back to quirks mode, which imitates the bugs of 1990s browsers so that very old pages still look the way they used to.

HTML
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>A modern page</title>
  </head>
  <body>...</body>
</html>

That short line is the right doctype for every new HTML document. It is case-insensitive (<!doctype html> is just as good), it is not an HTML element, and it has no closing tag. Older doctypes pointed at a DTD (a formal grammar file), but browsers never downloaded or used those files; they only looked at the text of the doctype to choose a rendering mode.

Rendering modes

Mode document.compatMode What it means
No-quirks (standards) "CSS1Compat" Follows the current HTML and CSS specifications. The mode you always want.
Limited-quirks ("almost standards") "CSS1Compat" Standards mode with one exception: an image alone in a table cell or line box doesn't get extra space below it for the text baseline.
Quirks "BackCompat" Emulates old browser behavior: different line heights, body stretching to the window height, tables not inheriting font sizes, unitless CSS lengths and hex colors without # accepted, and more.

Try it

The browser parses each snippet as a separate document and reports its mode. Edit the list or add your own doctypes.

Which mode does a doctype trigger?

compatMode cannot tell limited-quirks apart from full standards mode, which is why both show the same result. (Documents inside an <iframe srcdoc> always use standards mode, so this example uses DOMParser instead of the preview itself.)

The HTML doctype

Doctype Mode Notes
<!DOCTYPE html> No-quirks The doctype of the HTML Living Standard. Use it for all new pages.
<!DOCTYPE html SYSTEM "about:legacy-compat"> No-quirks Allowed for tools (mostly XSLT) that cannot output the short form. You never need to write it by hand.

HTML 4.01 doctypes

Version Doctype Mode
HTML 4.01 Strict <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> No-quirks
HTML 4.01 Transitional <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> Limited-quirks
HTML 4.01 Transitional, no URL <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> Quirks
HTML 4.01 Frameset <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> Limited-quirks
HTML 4.01 Frameset, no URL <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"> Quirks

The Transitional and Frameset doctypes switch mode depending on whether the second part, the system identifier (the DTD URL), is present. That rule let old pages that copied only half of the doctype keep their quirky layout.

XHTML doctypes

Version Doctype Mode (as HTML)
XHTML 1.0 Strict <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> No-quirks
XHTML 1.0 Transitional <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Limited-quirks
XHTML 1.0 Frameset <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> Limited-quirks
XHTML 1.1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> No-quirks
XHTML Basic 1.1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"> No-quirks

Most "XHTML" pages were actually served as text/html, so browsers parsed them as ordinary HTML and the table above applies. A page served as application/xhtml+xml is parsed as XML, where rendering modes don't exist: it is always in standards mode, with or without a doctype.

Older doctypes

Version Doctype Mode
HTML 4.0 Strict <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> No-quirks
HTML 4.0 Transitional <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> Quirks
HTML 3.2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> Quirks
HTML 2.0 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> Quirks
None at all โ€“ Quirks

How browsers pick the mode

The HTML parser decides once, when it reads the first token of the document:

  1. No doctype (or anything before it other than whitespace and comments) โ†’ quirks mode.
  2. A doctype whose name is not html, or one of a long list of old public identifiers (HTML 2.0, 3.x, 4.0 Transitional and Frameset, and various browser-specific DTDs) โ†’ quirks mode.
  3. HTML 4.01 Transitional or Frameset without a system identifier โ†’ quirks mode.
  4. XHTML 1.0 Transitional or Frameset, or HTML 4.01 Transitional or Frameset with a system identifier โ†’ limited-quirks mode.
  5. Anything else, including <!DOCTYPE html> โ†’ no-quirks mode.

Comparisons are case-insensitive, and the mode cannot be changed later by script.

Usage notes

  • Put the doctype first. Only whitespace and comments may come before it. A byte order mark is fine, but text, a stray character or an XML declaration (<?xml โ€ฆ?>) in front of it pushes old browsers into quirks mode and is invalid HTML.
  • Don't keep legacy doctypes "to be safe". Converting an old page to <!DOCTYPE html> can change its layout, so test it, but every new page should use the short doctype.
  • Check the mode in DevTools. Run document.compatMode in the console: "CSS1Compat" is what you want, "BackCompat" means quirks mode. Firefox also notes quirks mode in its page info.
  • Validators expect <!DOCTYPE html>. The HTML checker reports quirks-mode doctypes as errors and the old HTML 4.0/4.01 Strict and XHTML Strict doctypes as obsolete but permitted.