Una vecchia versione di questo documento è disponibile in italiano.
In order to deal both with content written according to Web standards and with content written according to legacy practices that were prevalent in the late 1990s, contemporary Web browsers implement various engine modes. This document explains what those mode are and how they are triggered.
This document covers mode switching as it applies to Firefox and other Gecko-based browsers, Safari, Chrome and other WebKit-based browsers, Opera, Konqueror, Internet Explorer for Mac and Internet Explorer for Windows and browsers that embed IE. Instead of referring to the names of browser engines, the names of the best-known browsers using each engine are used instead.
This document focuses on the mode selection mechanism and does not document the exact behaviors of each mode.
Here are the various modes:
text/html ContentThe choice of the mode for text/html content depends on doctype sniffing (discussed later in this document). In IE8, the mode also depends on other factors. However, by default even in IE8, the mode depends on the doctype for non-intranet sites that are not on a blacklist supplied by Microsoft.
It cannot be stressed enough that the exact behavior of the modes varies from browser to browser even though discussion in this document has been unified.
In the Quirks mode the browsers violate contemporary Web format specifications in order to avoid “breaking” pages authored according to practices that were prevalent in the late 1990s. Different browsers implement different quirks. In Internet Explorer 6, 7 and 8, the Quirks mode is effectively frozen IE 5.5. In other browsers, the Quirks mode is a handful of deviations from the Almost Standards mode.
If you are authoring new pages now, you are supposed to comply with the relevant specifications (CSS 2.1 in particular) and use the Standards mode.
In the Standards mode the browsers try to give conforming documents the specification-wise correct treatment to the extent implemented in a particular browser.
Since different browsers are at different stages of compliance, the Standards mode isn’t a single target, either.
HTML 5 calls this mode the “no quirks mode”.
Firefox, Safari, Chrome, Opera (since 7.5) and IE8 also have a mode known as “the Almost Standards mode”, which implements the vertical sizing of table cells traditionally and not rigorously according to the CSS2 specification. Mac IE 5, Windows IE 6 and 7, Opera prior to 7.5 and Konqueror do not need an Almost Standards mode, because they don’t implement the vertical sizing of table cells rigorously according to the CSS2 specification in their respective Standards modes anyway. In fact, their Standards modes are closer to Mozilla’s Almost Standards mode than to Mozilla’s Standards mode.
HTML 5 calls this mode the “limited quirks mode”.
IE8 has a mode that is mostly a frozen copy of the mode that was the standards mode in IE7. Other browsers do not have a mode like this, and this mode is not specified by HTML5.
application/xhtml+xml Content (XML Mode)In Firefox, Safari, Chrome and Opera, the application/xhtml+xml HTTP
Content-Type (not a meta element nor a
doctype!) triggers the XML mode. In the XML mode, these browsers give
the specification-wise correct treatment for XML documents to the extent
implemented in a particular browser.
IE 6, 7 and 8 do not support application/xhtml+xml. Neither
does Mac IE 5.
In the WebKit-based Nokia S60 Browser, the application/xhtml+xml
HTTP Content-Type does not trigger the XML mode due to concerns
of compatibility with ill-formed content in mobile walled gardens. (Legacy
“mobile browsers” don’t use a real XML parser and, therefore, ill-formed
content has been labeled as XML.)
I have not tested Konqueror sufficiently to say what exactly happens in that browser.
Some engines have modes that are not relevant to Web content. These modes are only mentioned here for completeness. Opera has a WML 2.0 mode. WebKit on Leopard has a special mode for legacy Dashboard widgets.
Here are the main effects of the modes:
The modes for text/html mainly affect CSS layout. For
example, not inheriting styles into tables is a quirk. In some browsers, the
box model changes to the IE 5.5 box model in the Quirks mode. This document does
not emumerate all the layout quirks.
In the Almost Standards mode (in browsers that have one), the height of table cells containing only images is computed differently compared to the Standards mode.
In the XML mode, selectors have different case-sensitivity behavior. Furthermore, special rules for the HTML body element do not
apply in older versions of browsers that do not implement the latest CSS 2.1 changes.
There are also some quirks that affect HTML and CSS parsing and would cause conforming pages to be misparsed. These quirks are toggled on and off with quirky layout. However, it is important to realize that the Quirks mode vs. Standards mode is predominantly about CSS layout and parsing—not HTML parsing.
Some people misleadingly refer to the Standards mode as “strict parsing mode”, which is misunderstood to imply that browsers enforced HTML syntax rules and that a browser could be used to assess the correctness of markup. This is not the case. The browsers do tag soup fix-ups even when the Standards mode layout is in effect. (In 2000 before Netscape 6 was released, Mozilla actually had parser modes that enforced HTML syntax rules. These modes were incompatible with existing Web content and were abandoned.)
Another common misconception is related to XHTML parsing. It is often thought using an XHTML doctype gains different parsing. It does
not. XHTML documents served as text/html are parsed using the same parser that is used for HTML. As far as browsers are concerned,
XHTML served as text/html is just “tag soup with croutons” (an extra
slash here and there).
Only documents served using an XML content type (e.g. application/xhtml+xml or application/xml) trigger the XML mode for parsing in which case the parser is totally different from the HTML parser.
Although the Quirks mode is primarily about CSS, there are some scripting quirks as well. For example, in Firefox’s Quirk mode, the HTML id attribute establishes object references from the global scripting scope as in IE. The effects on scripting are more dramatic in IE8 than in other browsers.
In the XML mode, some DOM APIs behave drastically differently, because the DOM API behavior for XML is defined to be incompatible with the HTML behavior.
Contemporary browsers use doctype
sniffing in order to decide the engine
mode for text/html documents. This means that the mode is picked based
on the document type
declaration (or the lack thereof) at the beginning of an HTML document.
(This does not apply to documents served with an XML content type.)
A document type declaration (doctype) is a syntactic artifact of SGML—a legacy markup framework that HTML prior to HTML5 was defined in terms of. In the HTML 4.01 specification, the document type declaration is said to communicate HTML version information. Despite the name “document type declaration” and despite what the HTML 4.01 specification says about “version information”, the document type declaration is not an appropriate means for classifying SGML or XML documents as documents of a particular type even though it seems it was supposed to be (hence the name). (More on this in the addendum.)
Neither the HTML 4.01
specification nor ISO 8879 (SGML) says
anything about using the document type declaration as an engine mode
switch. Doctype sniffing is based on the observation that at the time
doctype sniffing was devised the vast
majority of quirky documents either didn’t have a document type
declaration or they referenced an old DTD. HTML5 acknowledges this reality and
defines the doctype in text/html as a mode switch only.
A typical pre-HTML5
document type declaration contains (separated by
white space) the string “<!DOCTYPE”,
the generic identifier of the root element (“html”),
the string “PUBLIC”,
a public
identifier of a DTD in quotes, possibly a system identifier (an URL) of
the same DTD and the character “>”.
The document type declaration is placed in the document before the
start tag of the root element.
text/html
Here
are simple guidelines for choosing
a doctype for a new text/html document:
<!DOCTYPE html>
This is the right thing to do if you want to validate new features such as <video>, <canvas> and ARIA. Note that the HTML5 validity definition is still in flux. Please be sure to test your image alignment in Firefox, Safari, Chrome or Opera 9 or 10. Testing image alignment with Internet Explorer is inadequate however be sure to test in IE8, too.
<!DOCTYPE
HTML PUBLIC "-//W3C//DTD
HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
This doctype also triggers the standards mode, and the decade-old HTML 4.01 validity definition is stable. Please be sure to test your image alignment in Firefox, Safari, Chrome or Opera 9 or 10. Testing image alignment with Internet Explorer is inadequate however be sure to test in IE8, too.
<!DOCTYPE
HTML PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
This gives you the Almost Standards mode (and the full Standards mode in really old Mozilla releases). Please note that your layouts based on sliced images in tables are likely to break if you later move to HTML5 (and, hence, the full Standards mode).
No doctype.
Please don’t do this. Willfully designing for the Quirks mode will come and haunt you, your coworkers or your successors in the future—when no one even cares about Windows IE 6 anymore (already no one cares about Netscape 4.x and IE 5). Designing for the Quirks mode is a bad idea. Trust me.
If you still want to support Windows IE 6, it is better to apply specific hacks for it using conditional comments than to regress other browsers into the Quirks mode.
I am not recommending any of
the XHTML doctypes, because serving
XHTML as text/html is considered harmful. If you choose to use an XHTML
doctype anyway, please note that the XML declaration makes IE 6 (but not IE 7!) trigger the Quirks mode.
application/xhtml+xmlThe simple guideline for application/xhtml+xml is not to use a doctype at all. This way the page cannot be “strictly conforming” XHTML 1.0, but that does not matter. (Please see the Addendum below.)
It was announced on A List Apart that IE8 would use a meta element-based mode switch in addition to doctype being a factor in the mode choice. (See commentary by Ian Hickson, David Baron, David Baron again, Robert O’Callahan and Maciej Stachowiak.)
IE8 has four modes: IE 5.5 quirks mode, IE 7 standards mode, IE 8 almost standards mode and IE 8 standards mode. The choice of mode depends on data from various sources: doctype, a meta element, an HTTP header, periodically downloaded data from Microsoft, the intranet zone, settings made by the user, settings made by an intranet administrator, the mode of the frame parent if any and a UI button togglable by the user. (With other apps that embed the engine, the mode also depends on the embedding application.)
The lucky thing is that IE8 uses doctype sniffing roughly like other browsers if:
X-UA-Compatible HTTP header set by the author.X-UA-Compatible meta tag set by the author.For the points other than the two X-UA-Compatible cases, IE8 performs doctype sniffing like IE7. The IE7 emulation is called Compatibility View.
In the X-UA-Compatible cases, IE8 behaves radically differently from other browsers. Please see an appendix on this page or a flowchart available in PDF and PNG formats.
Unfortunately, without an X-UA-Compatible HTTP header or meta tag, IE8 lets the user accidentally drop you from the IE8 standards mode to the IE7 mode that emulates the standards mode of IE7 even if you used a proper doctype. Worse, an intranet admin may do this. Also, Microsoft may have blacklisted the entire domain you use (e.g. mit.edu!).
To counter these effects, a doctype isn’t enough and you need an X-UA-Compatible HTTP header or meta tag.
Here
are simple guidelines for choosing an
X-UA-Compatible HTTP header or meta tag for a new text/html document that already has a doctype that triggers the standards mode or almost standards mode in other browsers:
You don’t need to include an X-UA-Compatible HTTP header or meta tag.
Include either the following meta element (which in invalid in HTML5) on your page <meta http-equiv="X-UA-Compatible" content="IE=Edge"> (before any script elements!) or set the following HTTP header on your page: X-UA-Compatible: IE=Edge
First, include either the following meta element (which in invalid in HTML5) on your page <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"> (before any script elements!) or set the following HTTP header on your page: X-UA-Compatible: IE=EmulateIE7
Then fix your site not to rely on non-standard IE7 behaviors and migrate to IE=Edge.
Please don’t bring doctype sniffing to XML.
Doctype sniffing is a tag soup solution to a tag soup problem. Doctype sniffing was devised after the HTML 4 and CSS2 specs had been written as a heuristic way to distinguish legacy documents from documents whose authors might expect conforming behavior.
Sometimes it is suggested that doctype sniffing be used on the XML side as well for dispatching to different handlers, for recognizing the vocabulary in use, or for activating features. This is a bad idea. Dispatching and vocabulary recognition should be based on namespaces and feature activation should be based on explicit processing instructions or elements.
The whole concept of
well-formedness was introduced to allow DTDless
parsing of XML and, by extension, doctypeless documents. In formal
terms, if two XML documents have the same canonical form and an app
treats them differently (and the difference is not due to opting not to
process external entities), chances are the app is broken. In practical
terms, if two XML documents cause the same content to be reported
(qnames ignored) to SAX2 ContentHandler
and an app treats the documents differently, chances are the app is
broken. Considering that as a Web author you cannot trust that everyone
parsing your pages uses an XML processor that resolves external
entities (even if some browsers appear to do so because they map certain public ids to an abridged DTD for entity definitions), inserting a doctype in XML intended for the Web is
mostly pointless and often done out of a cargo cultish habit. (You can
still validate against a DTD using the DTD override feature of the
W3C Validator, although the W3C Validator will say that the result is only tentatively valid. Or better yet, you can use RELAX NG validation, which does not
pollute the document with schema references.) Requiring
a doctype only for sniffing would be silly, even though that is the situation
with HTML in practice.
Moreover, when a lower-level
spec defines two things that are
equivalent, a higher-level spec should not try to give different
meanings to the two things. Consider <!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">.
If the
public identifier is removed, the same DTD is still designated and,
therefore, the doctype <!DOCTYPE
html SYSTEM
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
means
the same as the previous doctype. Should they be sniffed differently?
The idea can be carried further. Suppose the DTD is copied to
example.com and named foobar.dtd: <!DOCTYPE
html SYSTEM
"http://example.com/foobar.dtd">.
How could that one be
sniffed? The meaning is the same. The whole DTD could even be pasted
inline!
To put it another way, if you
have #include "foo.h",
you should not bind any black magic to the name foo.h,
because it should be permissible to paste the contents of foo.h
inline or copy the contents of foo.h
to bar.h
and say #include "bar.h".
The reason I don’t bother making the same argument with HTML and SGML is that Web browsers don’t use real SGML parsers for parsing HTML, so I don’t think it is useful to pretend that SGML is being dealt with. However, if you are not convinced yet, please see W. Eliot Kimber’s comp.text.sgml post about the matter.
text/htmlIn the following table, Quirks Mode, Standards Mode and Almost Standards Mode are denoted by Q, S and A, respectively. When a browser only has two modes, the Standards Mode is marked as “S”, if the line height in table cells works as in Mozilla’s Standards Mode, and as “A”, if the line height in table cells works as in Mozilla’s Almost Standards Mode.
Please note that XHTML served using an XML content type is rendered in the XML mode.
The purpose of this table is not to suggest that all the doctypes listed in the table are reasonable choices for new pages. The purpose of this table is to show what data I am basing my recommendations on.
The following shorthand notation is used in the column headers:
X-UA-Compatible override nor a Compatibility View override (“A” means the IE8 Almost Standards mode in this case), Opera 7.5…8.54 and 9.5…9.6X-UA-Compatible override (in this case “A” means the IE7 mode) and Opera 7.10…7.23 Mozilla’s doctype sniffing code has changed substantially in October 2000, in September 2001 and in June 2002. This document describes the situation with Mozilla builds made available at ftp.mozilla.org on 2000-10-19 and later (and with Netscape 6.x). This document does not address the way doctype sniffing worked in Mozilla M18 (and Netscape 6.0 PR3). Safari’s doctype sniffing code has also changed substantially since the first public beta. This document does not cover the behavior of versions earlier than v73 aka. 0.9.
Konqueror’s doctype sniffing code prior to version 3.5 appears to come from a very early version of Safari. Konqueror now matches Safari whose doctype sniffing code comes from Mozilla.
As can be seen from the table, Opera’s doctype sniffing has gradually moved from being IE-like to towards being Mozilla-like, although Opera 9.5 and 9.6 regressed on the way. At the same time, the layout behavior of Opera’s Quirks mode has shifted from imitating the Quirks mode of IE 6 towards the Quirks mode of Mozilla.
Start: Go to “X-UA-Compatible meta?”.
meta?
script first: Go to “X-UA-Compatible HTTP Header?”.
The steps are available as a flowchart in PDF and PNG formats.
Thanks to Simon Pieters, Lachlan Hunt and Anne van Kesteren for their help with correcting the mode table for various Opera versions and for their comments. Thanks to Simon Pieters for the text alternative for the IE8 flowchart.