分享

What happens in Quirks Mode in web browsers?

 埃德温会馆 2013-04-09

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 brows-ers, es-pe-cial-ly 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 con-tains a doctype declaration as re-quired by HTML specifications.

The purpose of Quirks Mode is to make old pages to display as their author in-tend-ed. Old pages may have been written to utilize known features of old browsers or at least to adapt to them. For more in-for-ma-tion 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 com-posed this document. (Browser vendors do not generally document Quirks Mode. There are some descriptions, such as The effects of quirks mode emulation about IE, by Microsoft, but such descriptions cover some aspects only.)

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 re-ly-ing 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 ac-cord-ing to HTML and CSS specifications; this includes using a doctype dec-la-ra-tion that puts mod-ern browsers into Standards Mode. More-over, put the doctype dec-la-ra-tion 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 box model is incorrect (different from CSS spec-i-fi-ca-tions, though perhaps intuitively more natural). This means that the width and height properties specify the dimen-sions of the entire element box, including padding and border, and not just the element’s content. (There is a demo later in this document.)
  • Dimensions of non-replaced inline elements (e.g., span elements by default) are affected by width and height properties (while by CSS specifications, they shall be ignored).
  • Percentage heights for elements (e.g., <table height="100%"> in HTML or table { height: 100% } in CSS) are applied, using the available height as reference, even when the enclosing block has height: auto (the default). In Standards Mode, the height is determined by the require-ments of the content; but percentage heights work when the enclosing block has its height set to a specific value.
  • Textarea widths are calculated differently. E.g., on IE 9, cols=5 makes the area 5 characters wide in Standards Mode but only 4 characters wide in Quirks Mode.
  • Overflow is treated by expanding a box. When the content of an element does not fit into the dimensions specified for it (explicitly or implicitly), then overflow: visible (the default) means that the content overflows while the box dimensions are as specified. In Quirks Mode, the dimensions change; this can easily been seen e.g. if the box has a back-ground color or a border.
  • The root element is the body element on IE in Quirks Mode. By the specifications, it is the html element. For example, setting a margin or width for body has no effect in Quirks Mode. As another example, IE has a vertical scrollbar by default, though it is inactive (dim) when there is nothing to scroll, and you can remove it (when it is not needed) in Quirks Mode by setting body { overflow: auto; }, but in Standards Mode, you need to add html { overflow: auto; } as well.
  • A gray 2px page border appears by default on some versions of IE. It can in some cases be changed or removed by setting the border property of the html element (e.g., html { border: 0; }).
  • The id attribute values are matched case-insensitively, so that e.g. the CSS selector #foo matches an element with id="Foo". By the specifications, the match must be case-sensitive: #foo matches only an element with id="foo".
  • Padding for an image is ignored when set in CSS for an img element or an input type="image" element.
  • Default horizontal margin for a floated image is three pixels (instead of zero). That is, if the img element has the attribute align="left" or align="right" or if the CSS rule float: left or float: right applies to it, the browser behaves as if the img element had the attribute hspace="3" (or its margin-left and margin-right properties had the value 3px). This applies to IE; on other browsers, Quirks Mode may cause the extra margin on one side of the image only, and its width can be 2 pixels instead of 3.
  • Vertical alignment of an image is under certain conditions to the bottom of the enclosing box, not to the baseline of text. This happens when the image is the only content within an element, typically a table cell. This means that e.g. an image in a table cell is by default at the bottom of the cell in Quirks Mode (which is often what the author wants), whereas in Standards Mode there is a few pixels spacing below the image (unless one sets e.g. vertical-align: bottom for the img element).
  • Text rendering on IE 9 does not use several new features in the browser, when in Quirks Mode. This may e.g. make the width needed by some text different in different modes, thereby affecting the division of text into lines. See Microsoft’s About Text Rendering in Windows Internet Explorer 9.
  • Centering a block in CSS using e.g. margin: 0 auto does not work. Note: On IE up to IE 7, setting align="center" in HTML or text-align: center in CSS for a div incorrectly centers any inner block element as a whole, even in Standards Mode. On IE 8, this happens in Quirks Mode only,
  • Font properties are not inherited from body or other enclosing elements into tables. This happens especially for font-size but may happen for font face, color, and line height as well. For example, if you set body { font-family: Arial }, it is possible that the font in table cells remains the browser default.
  • In a font size setting for a table cell, a percentage value is interpreted as relative to the browser’s basic font size, not to the font size that applies to the enclosing element (the table row) by CSS specifications.
  • Font size keywords are interpreted incorrectly so that medium is larger than the browser’s basic font size and small equals that basic font size. Similarly, the entire scale of keywords, xx-small, x-small, small, large, x-large, xx-large is interpreted systematically wrong: each value is interpreted as one step larger than it should.
  • Font rendering is different on IE 9. In Standards Mode, it applies more advanced technologies. This causes small difference that may matter, affecting the width of text among other things. See About Text Rendering in Windows Internet Explorer 9.
  • Border width keywords thin, medium, and thick have different meanings on IE. For example, thin is 1px in Standards Mode, 2px in Quirks Mode.
  • The bordercolor attribute, which is nonstandard but widely supported, is treated differently in Quirks Mode vs. Standards mode by IE. The colors appear differently, though this seems to be caused by an effect on border style.
  • Radio buttons and checkboxes have total dimensions 20 by 20 pixels in IE in Quirks Mode, as opposite to 13 by 13 in Standards Mode and in other browsers. The size of the graphic button or box is the same, but the spacing around it is different. It is an integral part of the element rendering, not affected by padding or margin properties, but affected by width and height properties.
  • Malformed property values are often interpreted on the basis of guesswork, e.g. margin: 10 as margin: 10px and color: ffffff as color: #ffffff. This violates mandatory error processing rules in CSS: a rule using syntactically incorrect value shall be ignored.
  • Incorrect property values that are not recognized by a browser may cause an earlier setting for the property to be ignored. For example, setting h1 { color: red; color: nonsense; } may cause the entire rule to be ignored; by the specifications, the correct setting color: red shall prevail. Such duplicate settings are often used in an attempt to provide a fallback when using advanced CSS values, and this often fails in Quirks Mode, because the fallback gets ignored. The problem only appears within a CSS rule, so dividing a rule into two parts would help. For example, instead of h1 { color: #ccc; color: rgba(255, 255, 255, 0.7); } you would use h1 { color: #ccc; } h1 { color: rgba(255, 255, 255, 0.7); }
  • Case of letters is treated as insignificant in class and identifier selectors in CSS. Thus, the selector .foo matches an element with class="Foo" or class="FOO". By CSS specifications, the case is significant in these contexts.
  • Malformed names are accepted in class and identifier selectors. Specifically, names starting with a period or a number sign (e.g. in selectors .123a and #42) are accepted.
  • The declaration white-space: pre is ignored.
  • Fixed positioning is not supported: the declaration position: fixed is treated as position: static (on IE 7).
  • Many standard (CSS 2.1) additions to CSS support (such as the max-width property and attribute selectors) in IE 7 are not in use Quirks Mode. That is, there are many CSS features that were not supported in IE 6 and are supported in IE 7, but only in Standards Mode. See Microsoft’s blog entry Details on our CSS changes for IE7.
  • Some nonstandard CSS features (features not in CSS 2.1) such as scrollbar properties are recognized by some versions of IE and Opera in Quirks Mode but ignored in Standards Mode. On IE 8 and newer, this also applies to the expression() construct.
  • Setting rowspan=0 or colspan=0 is ignored in many browsers, even if they support these settings in Standards Mode. (They mean that the cell spans the rest of the row or column. This was implemented in browsers relatively lately.)
  • Tag soup parsing. For example, if a document contains the markup
    <p>text<table>...</table>
    then e.g. Firefox treats, in Quirks Mode, the p element as containing the table element. In Standards Mode, the start tag of table implicitly closes the open p element. The difference can be seen if you e.g. set a border on the p element. Similarly, for example, Firefox accepts a ul element inside a font. IE always works by wrong rules in such issues, even in Standards Mode, but standards-conforming behavior can be achieved by using valid markup and always using explicit end tags like </p> even when they are formally optional.
  • White space between elements can be significant. For example, let’s say you have a list of links. Typically you would write the markup with line breaks between the list items, the li elements (that is, between </li> and <li> tags):
    <ul>
    <li><a ...>...</a></li>
    <li><a ...>...</a></li>
    ...
    </ul>
    
    However, if you set display: block and a border for the link elements, you may wind up with vertical gaps (empty lines) between the boxes. This happens on IE 7 in Quirks Mode, and always on previous versions of IE. On the other hand, for the markup above, if you set display: inline for the li elements, then there is no white space between the element boxes on IE in Quirks Mode. In Standards Mode, and on other browsers, there is white space corresponding to one blank, and this is regarded as standards-conforming by some.
  • Forms have a default bottom margin of 1em in Mozilla browsers. (On IE, there is such a margin in both modes.) This is apparently meant to continue the tradition of web browsers to leave that much spacing below a form. It used to be a frequently asked question how to get rid of the empty line after a form.
  • Default vertical margins are suppressed in some contexts, e.g. when a p element appears as the first element in a td element. This is more or less traditional behavior in browsers and takes place on IE 7 in both modes (on IE 8, in Quirks Mode only). The issue does not arise if the relevant elements have their vertical margins set explicitly in CSS.
  • Tooltip effects for img elements are based on alt attributes on some old-style browsers, including IE 8 in Quirks Mode but not in Standards Mode.
  • The wbr markup for a line breaking opportunity is honored. Browsers have widely supported it, but IE 8 has stopped doing so in “standards mode.” This is bad news, since <wbr> tags have been the only effective way to suggest line break opportunities for otherwise unbreakable strings on web pages. (Worse still, IE 9 seems to refuse to recognize it in any mode.)
  • Mozilla browsers (like Firefox and Seamonkey) have some additional features, documented in the quirk.css file. For example, the default color for table borders is gray (as in most other browsers), as opposite to using the foreground color of the table as in Standards Mode. Note: Not all the settings in that file apply to all versions of Mozilla.
  • Some DOM features are not available. For example, in Quirks Mode, IE 9 does not recognize the getElementsByClassName method (which is well supported by other browsers and included in HTML5 drafts).
  • The so-called named access on the Window object such as using (in JavaScript) window.foo, window['foo'] or just foo to refer to the element with id="foo" is possible in Quirks Mode only in some versions of Firefox. (In version 15, it however restored support to this legacy feature in both modes.)

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 con-tains 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 issue).

An IE bug triggered end tag omission in “Standards Mode”

A paragraph with no end tag </p>.

A form that should have yellow background.

Rendering on IE 7 in “Standards Mode”:

A wrong appearance as described in text.

On IE 7, if a form element is preceded by a p element (a paragraph), omitting the optional end tag </p> may mess up some features of styling. If the form has a background set for it, the background may appear as a thin hor-i-zon-tal stripe only, on the top. If the form has a border set for it, it may be drawn incorrectly, excluding some part of the form. There is a simple separate test page for this IE 7 bug. This bug has been observed on Opera, too. Whether the bug is triggered seems to depend on how the content of the form starts, in terms of markup.

This bug does not exist in previous versions of IE.

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.)

In IE 8 and IE 9, this bug still exists, in different manifestations. When viewing the test page in Stan-dards Mode, IE 8 shows a bordered thin yellow stripe as if there were an extra empty form before the real form. IE 9 does the same but displays the real form without background color and without border.

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,
  • On Firefox, use the command View/Page Info (and see the General pane); or if you have Web Developer Extension, just check an icon in its toolbar (third icon from the right).
  • For IE, type javascript:alert(document.compatMode) in the address bar, and check whether the popup window then says CSS1Compat (indicating Standards Mode) or BackCompat (indicating Quirks Mode); alternatively, download and install the simple Quirks or Standards Mode Bookmarklet.

IE 8 “compatibility mode” is different

IE 8 has a “compatibility mode” icon in its address bar in many situations. The icon appears on the right of the address (URL) of the page being visited and it looks like a torn-down sheet of paper.

The meaning and use of this icon, as well as the compatibility mode selection in the Page menu of IE 8, is obscure. According to tooltip and help texts, clicking on the icon toggles between compatibility mode and normal mode. However, contrary to what we might expect, this is not at all the same as switching between Quirks Mode and Standards Mode. Some features of rendering may be affected, but crucial issues such as box model are unaffected; they are based on the doctype declaration. When it causes Standards Mode, the “compatibility mode” means that IE 8 emulates IE 7 in some issues. In Quirks Mode, “compatibility mode” means emulating IE 5, more or less, See Microsoft’s complicated explanation of this feature and their page Defining Document Compatibility.

IE 9: more confusion

IE 9 has seven different rendering modes, with different intermediate modes between Quirks Mode and Standards Mode. Using a correct DOCTYPE does not guarantee Standards Mode; for various reasons, a page may still be rendered in a manner that simulates IE 7 – though this isn’t nearly as bad as Quirks Mode. Check Henri Sivonen’s page Activating Browser Modes with Doctype for the whole story.


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多