Quirks mode

On this page 6

Quirks mode is a document rendering mode in which a web browser deliberately reproduces legacy, nonstandard layout and CSS behavior so that pages written for late-1990s browsers still display as their authors intended. Among browser technologies on the Web, it is one of three modes the rendering engine chooses before it lays out a single element, and the choice depends almost entirely on the DOCTYPE declaration at the top of the HTML document.

The other two modes are no-quirks mode, often called standards mode, and limited-quirks mode, historically called almost standards mode. Modern pages should always run in no-quirks mode, because quirks mode changes sizing, selector matching and scrolling in ways that are hard to debug.

How the browser selects quirks mode

The HTML parser decides the mode once, while it reads the DOCTYPE token, and the decision cannot be changed later by scripts or style sheets. The rules are specified in the WHATWG HTML Living Standard, so Blink, WebKit and Gecko now trigger the modes almost identically.

The short declaration <!DOCTYPE html> always produces no-quirks mode. A missing DOCTYPE, a malformed one or a list of legacy public identifiers from HTML 2.0, HTML 3.2 and early HTML 4 drafts produce quirks mode. The table below shows typical cases.

Document startResulting modeNote
<!DOCTYPE html>No-quirksRecommended for all new pages
No DOCTYPE at allQuirksMost common accidental trigger
HTML 4.01 Transitional without system identifierQuirksLegacy identifier list
HTML 4.01 Transitional with system identifierLimited-quirksOnly table cell line height differs
XHTML 1.0 TransitionalLimited-quirksSame narrow difference
Comment or text before DOCTYPEQuirks in old IEModern parsers tolerate comments
DOCTYPE declarations and the resulting mode

Some documents never enter quirks mode: XML documents served as XHTML, and iframes created with the srcdoc attribute, which are always rendered in no-quirks mode. Each iframe has its own mode, so a standards-mode page can embed a quirks-mode frame.

Behaviors that change in quirks mode

Quirks mode is not a single switch but a catalog of specific deviations, collected in the WHATWG Quirks Mode specification and in the CSS specifications. Each quirk exists because widely used old pages depended on it, and removing it would break them.

The following table lists the deviations that most often surprise developers.

QuirkBehavior in quirks modeBehavior in no-quirks mode
Percentage heightsResolve against the viewport through auto-height ancestorsCompute to auto when the parent height is auto
Body heightBody fills the viewportBody is as tall as its content
Class and ID selectorsMatched case-insensitivelyMatched case-sensitively
Unitless lengthswidth: 200 is read as pixelsDeclaration is ignored as invalid
Hashless colorscolor: ff0000 is acceptedDeclaration is ignored as invalid
Table font inheritanceTables do not inherit font sizeTables inherit normally
Scrolling elementdocument.body scrolls the pagedocument.documentElement scrolls the page
Common quirks and their effect

Historically, Internet Explorer in quirks mode also used its old box model, in which declared width included padding and border. Modern engines do not apply that model to all elements; the equivalent behavior is now available deliberately through box-sizing: border-box.

Quirks mode compared with the other rendering modes

The three modes differ in how much legacy behavior they keep. Limited-quirks mode was introduced to fix one painful problem: in strict mode, images inside table cells gained a gap below them because inline images sit on the text baseline, which broke sliced-image layouts.

Modedocument.compatMode valueScope of legacy behavior
No-quirks modeCSS1CompatNone, follows current CSS specifications
Limited-quirks modeCSS1CompatOnly the line height quirk in table cells
Quirks modeBackCompatFull set of documented quirks
The three document modes

Because limited-quirks mode reports the same value as no-quirks mode, `document.compatMode` cannot distinguish them. Developer tools in Chromium and Firefox show a console warning when a page renders in quirks mode, which is the fastest way to detect the problem.

Quirks mode should also not be confused with browser compatibility views such as the old Internet Explorer document modes, which emulated entire earlier engine versions. Quirks mode is far narrower: the same modern engine runs the same JavaScript and supports the same CSS features, and only the listed legacy deviations are switched on. Performance and security are therefore unaffected; only layout and a few DOM properties change.

Why quirks mode still matters for developers

Quirks mode remains relevant because templates, email-generated pages, intranet systems and server-side includes frequently omit the DOCTYPE or emit output before it. The resulting bugs look like CSS errors, so teams often spend hours on layout fixes that address the symptom instead of the cause.

The table below connects typical symptoms with their cause and fix.

SymptomLikely causeFix
Height 100% works unexpectedlyPercentage height quirkAdd <!DOCTYPE html> and set explicit heights
Scroll position reads zeroWrong scrolling elementUse document.scrollingElement
Selectors match mixed caseCase-insensitive matchingFix the DOCTYPE and correct names
Fonts shrink inside tablesNo font inheritance in tablesFix the DOCTYPE first
Framework layout breaksLibrary assumes standards modeRemove output before the DOCTYPE
Symptoms of an unintended quirks mode
  • Place the DOCTYPE on the first line, before any whitespace emitted by templates where possible.
  • Check `document.compatMode` in automated tests to catch regressions.
  • Do not rely on quirks deliberately, because they are poorly documented outside the specifications.
  • Remember iframes separately, since each embedded document has its own mode.

In everyday language

Web pages start with a short label that tells the browser which set of rules the page was written for. If that label is missing or very old, the browser assumes the page comes from the early days of the Web and switches on an old compatibility behavior called quirks mode.

It is a little like a modern car offering a setting that mimics an older model so that an old trailer still fits. Useful for very old pages, but new pages should never use it. One correct line at the top of the file avoids the problem.

Seen in the real world

A developer at a logistics company moved an internal dashboard to a new component framework. Every panel that used height: 100% collapsed, and a sticky header jumped on scroll, even though the same components worked in the framework’s demo pages. Two days went into adjusting CSS without success.

The developer then opened the browser console and saw a quirks mode warning. The legacy server template printed a hidden status comment and a tracking line before the HTML, and no DOCTYPE was emitted at all. After the template was changed to output <!DOCTYPE html> first, document.compatMode reported CSS1Compat, the panels rendered correctly and about 40 lines of workaround CSS were deleted.