What happens in Quirks Mode?

Quirks Mode is a mode of operation of web browsers such as Internet Explorer (IE), Firefox, and Opera. Basically, Quirks Mode (also called Compatibility Mode) means that a relatively modern browser in­ten­tio­nal­ly simulates many bugs in older browsers, especially IE 4 and IE 5.

Quirks Mode is triggered by doctype sniffing, also known as doctype switching. This means that the browser inspects the start of an HTML document to see whether it contains a doctype declaration as required by HTML specifications.

The purpose of Quirks Mode is to make old pages to display as their author intended. Old pages may have been written to utilize known features of old browsers or at least to adapt to them. For more information about Quirks Mode in general, consult the QuirksMode.org site.

There is no authoritative specification of what happens in Quirks Mode. After all, the mode is, by essence, an inten­tional violation of CSS and HTML specifications. However, since authors may need a description of what may actually happen in Quirks Mode, I have composed this document.

If you have an existing page that seems to work well but lacks a doctype dec­la­ra­tion (required by HTML specifications) at the beginning, you should not simply put such a dec­la­ra­tion there. The reason is that the dec­la­ra­tion makes browsers work in the so-called Standards Mode as opposite to Quirks Mode. This may mean just about anything. I have seen the entire content of a page disappear when you add a doctype dec­la­ra­tion. More often, the layout changes in rather un­ex­pect­ed ways. Well, the ways are not that un­ex­pect­ed if you know what may happen in Quirks Mode.

Before adding a doctype dec­la­ra­tion, you should check both the HTML and CSS code for syntactic correctness by using validators and checkers. This may not be enough, since the page might still have been written relying on things that just “work”—in Quirks Mode. Thus, you should test the page at least on IE 7 and Firefox 2 in Standards Mode, i.e. after adding a doctype dec­la­ra­tion. If the page doesn’t work then, the following list might be useful for spotting the problem.

When creating a a new page, you need not know about Quirks Mode and should usually not think about it. Simply write according to HTML and CSS specifications; this includes using a doctype dec­la­ra­tion that puts modern browsers into Standards Mode. Moreover, put the doctype declaration at the very start, since some browsers go to Quirks Mode, if there is anything (even a comment) before it. (This causes trouble if you are using XHTML, but in most cases, you shouldn’t use XHTML for web pages anyway, for the time being.) But if you decide to use some features that might only work in Quirks Mode, such as a height="100%" attribute for a table element, you should check the list for other possible implications.

In Quirks Mode, browsers have been observed to behave in the following ways, though not all browsers exhibit all of these features:

The list is most probably not exhaustive. It relates mainly to IE 7. Other browsers may have a Quirks Mode that does simulate old versions of IE to the same extent.

Simple demo

The following simple images demonstrate one of the many differences between Quirks Mode and Standards Mode on Internet Explorer, namely the box model. They are presentations of the following markup in the two modes:
<div style="border: solid 2px #006; width: 8em; padding: 0 2em">stuff</div>

(A box with the text "stuff" inside, 134 pixels wide) (Quirks Mode)
(A box with the text "stuff" inside, 203 pixels wide) (Standards Mode)

Here is the element as presented by your current browser, for a quick check:

stuff

The explanation is that in Quirks Mode, the width property is (incorrectly) taken as specifying the total width of the element’s box, 8em in this case. In Standards Mode, it specifies the width of the content of the element, so that the total width of the box 12em (plus the widths of the borders). That is, the total width contains the left and right padding, each set to 2em.

On Firefox, the correct box model is applied in both modes. However, you can still simulate the incorrect box model used by IE in Quirks Model, by using the command CSS/Use Border Box Model in Web Developer Extension (which is a great tool in any CSS-related issues and many more).

An oddity in IE 7 and IE 8

A paragraph with no end tag </p>.

A form that should have yellow background.

On IE 7, if a a form element is preceded by a p element (a paragraph), omitting the optional end tag </p> tends to mess up some features of styling. If the form has a background set for it, the background will appear as a thin hor­i­zon­tal stripe only, on the top. There is a simple separate test page for this IE 7 bug. This bug has been observed on Opera, too.

The surprising thing is that this bug does not exist in previous versions of IE and, even more strangely, it exists in “Standards Mode” only. In Quirks Mode, the back­ground is shown properly, though of course other Quirks Mode features may mess up a page.

Presumably, this bug is connected with the fake “XHTML orientation” in IE 7. This browser version, despite actually not supporting XHTML, imposes some XHTML-like restrictions on (non-XHTML) HTML documents! In XHTML, the end tag </p> is not optional. Another bug of this kind is that some valid entity references like &Omega are not interpreted properly but displayed literally by IE, since they lack the terminating semicolon, which is always required in XHTML. (This bug appears in both Standards Mode and in Quirks Mode.)

IE 8 does not exhibit this bug in some cases, but viewing the test page shows even stranger behavior. In Standards Mode, the browser shows a thin yellow stripe as if there were an extra empty form. In Quirks Mode, the stripe is not there but the form lacks yellow background, except for a narrow stripe at the very start.

The conclusion is that the old recommendation to use explicit end tags is very sound. Throughout the history of HTML, various browsers have failed to infer the closing tags properly, and there is probably no end to this madness.

Checking the mode

To check which mode (Quirks vs. Standards) a browser is in,