Don't use the XML declaration with XHTML

From Jonathan's Reference Pages


If you build webpages you may have encountered webpages that begin with this line:

<?xml version="1.0" encoding="UTF-8"?>

It's called the "XML prolog" or "XML declaration", and should not be used in webpages for several reasons:

  1. It's misinterpreted by Internet Explorer 6's doctype sniffing, and this significantly affects page rendering.
  2. It's not required, and XHTML 1.0 is valid without it.
  3. It adds no extra functionality to most webpages.
  4. It encourages other HTML writers to include the XML declaration, without warning them of the issues in IE6

More detailed explanation

If you're familiar with Quirks vs Standards modes (and if you work with webpages, you should be), you'll know that modern web browsers use the <!DOCTYPE declaration to determine how correctly a webpage should adhere to the W3C standards. Historically, Internet Explorer has implemented these standards poorly, hence the need for doctype switching.

Unfortunately, there's a bug: Internet Explorer 6 only scans the first non-whitespace element in the document when looking for the doctype. Since the XML declaration is not a valid doctype, it assumes that this document has no doctype declaration and reverts to Quirks Mode.

Worse still, webpages with the XML prolog usually include a valid doctype, giving many developers the impression that their page is triggering Standards Mode in IE6. It will trigger Standards Mode in most other browsers, including IE7 which fixes this bug.

It's true that the W3C recommendations strongly encourage including an XML declaration. Unfortunately, as long as web developers need to support Internet Explorer 6, inclusion is not an option. Thankfully, since the line is not normally required and has little or no effect, omitting it is not a major issue.


Page created: 23rd September 2008