<!DOCTYPE html> is not a version declaration and the browser never fetches a DTD. The HTML standard calls it a "required preamble" kept for legacy reasons: its only job is to choose the document's mode.
The DOM gives every document one of three modes, set by the parser's "initial" insertion mode when it sees the first token:
- no-quirks (standards mode): the doctype is
<!DOCTYPE html>(any case), optionally withSYSTEM "about:legacy-compat". - limited-quirks: a few transitional doctypes, such as XHTML 1.0 Transitional. Only the line-height quirks apply, so an image alone in a table cell has no gap below it.
- quirks: no doctype, a doctype whose name isn't
html, or one of 61 listed legacy identifiers (HTML 3.2, HTML 4.01 Transitional without a system identifier, and so on).
The doctype must come first. Comments and whitespace before it are fine; any text or element before it means the parser saw "anything else", which is a parse error and sets quirks mode.
What quirks mode changes is collected in the Quirks Mode Standard, plus a few rules in other specs:
- CSS accepts
width: 200(unitless length) andcolor: ff0000(hashless hex). - Percentage heights look past
auto-height ancestors, andbodystretches to fill the viewport. - Tables reset inherited font properties (HTML's quirks-mode UA styles), and text decoration doesn't propagate into them.
- Class and ID selectors match case-insensitively (Selectors 4).
document.scrollingElementisbody, nothtml(CSSOM View).
You can read the mode from document.compatMode: "BackCompat" for quirks, "CSS1Compat" for both no-quirks and limited-quirks. Every new page should start with <!DOCTYPE html>; quirks mode exists so pages from the 1990s keep rendering as their authors saw them.