Choosing the proper DOCTYPE

06 Mar 2009 by David Corvoysier

I have been recently asked to tell the differences between HTML 4.0, XHTML 1.0 and HTML 5.0, and I had to dig in the specifications to provide an answer. Doing this, I realized that I did not really pay attention to the DOCTYPE declarations in my web pages so far, possibly leading to unexpected behaviours in some browsers (see the very detailed article on Wikipedia about Quirks mode).

To be short, HTML documents must start with a DOCTYPE declaration that is used by the browser to choose the appropriate rendering. It allows in particular to distinguish between real HTML documents and XHTML documents served as text/html to non-XHTML browsers (though required by the XHTML specification, the DOCTYPE declaration is actually redundant with the namepace declaration if the document is served as application/xhtml+xml).

The current HTML specification is HTML 4.01 (1999). It defines three document types:

  • HTML 4.01 strict, that relies entirely on CSS for presentation
  • HTML 4.01 transitional, that still allows deprecated presentational markup
  • HTML 4.01 frameset, that must only be used to declare framesets

In a first attempt to replace HTML 4.01, the W3C has defined XHTML 1.0 (2000-2002), that redefines HTML 4.01 using a strict XML syntax. Unsurprisingly, XHTML 1.0 also defines three document types:

  • XHTML 1.0 strict, that is exactly HTML 4.01 strict with closing tags,
  • XHTML 1.0 transitional, that is exactly HTML 4.01 transitional with closing tags,
  • XHTML 1.0 frameset, that must only be used to declare framesets.

In a later standardization effort, the W3C produced XHTML 1.1 (WIP), that doesn't add much, but defines only a strict DOCTYPE. It is no offense to the promoters of XHML 1.1 to say that it hasn't yet been widely adopted (and to be honest it will probably go to oblivion soon since HTML 5 is on its way).

The upcoming HTML 5 specification takes a different approach and uses a single DOCTYPE for both HTML and XHTML documents (the DOCTYPE is optional for XHTML documents because it is redundant with their mime-type information: application/xhtml+xml).

To summarize, when developing a new page, the first thing you need to decide is if you want it to be HTML 5 or not.

If you go for HTML 5, then it is pretty straightforward: just add the following line at the top of your file:

<!DOCTYPE html>

If you want to address the legacy browsers, or if for any other reason you don't want to use HTML 5, then you need to decide whether you are going to use HTML or XHTML. My recommendation is to always choose HTML unless you really need XHTML (for instance because you want to add inline SVG).

Then, in both cases, unless you really need to use the deprecated HTML syntax, use the strict DOCTYPE.




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">

Please note that according to the W3C XHTML guidelines, the XML declaration () should be omitted, as it triggers quirks mode in older versions of Internet Explorer. This is only a problem if you need to specify a different character encoding than UTF-8 (the default).

comments powered by Disqus