2.1 Document Type Declaration (DTD)

The Document Type Declaration (DTD), sometimes called DOCTYPE, informs the browser which type of (x)HTML is being used out of all of those available and is included at the top of a .html document. In addition, some extra information is added to the html tag with two attributes called xmlns and lang.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
 <head>
  <title>Title</title>
 </head>
 <body>
  <p>Content text</p>
 </body>
</html>

The xmlns attribute stands for XML-Name-Space and has the value http://www.w3.org/1999/xhtml.

The lang attribute states which language the document is written in, taken from ISO 639, which lists codes for all the world languages. In our case the language is set to English ("en").

Standards and Quirks Mode

Because early browsers didn't support the W3C and IETF standards (or even comply with each other), and to ensure that web pages rendered correctly, it was soon realised that browsers needed 2 modes to interpret CSS:

  • Quirks mode for the old rules
  • Standards mode to meet the w3C standards.

In addition there is now an Almost Standards mode that matches standards mode except that the layout of images in table cells is implemented in quirks mode rather than in accordance with the CSS2 specification. Quirks and Almost Standards modes are all about backward compatibility. IE Mac was the first browser to implement the quirks and standards modes, and IE6, Mozilla, Safari, and Opera soon followed suit. Older browsers such as IE5 and Netscape 4 are locked in quirks mode. Almost standards mode has since been implemented in IE8, Safari, opera 7.5 onwards, and Mozilla Firefox.

DOCTYPE Switching

Browsers generally determine which mode to use based upon the presence of a Document Type Declaration (DTD) at the top of the page. This trigger for choosing which mode to use is called DOCTYPE switching (or sniffing). If a full DOCTYPE is present the browser will use standards mode. If it is missing the browser will use quirks mode. For example, the following DOCTYPE would trigger standards mode:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/DTD/strict.dtd">

The following DOCTYPE doesn't state the version of HTML being used nor the URL of an HTML Document Type Definition, and so would trigger quirks mode:

<!DOCTYPE html PUBLIC>

An exception to this rule is the ever troublesome IE6. If a DOCTYPE that triggers strict mode is preceded by an XML prolog, quirks mode is triggered. This rule was implemented by Microsoft to allow web developers to achieve valid pages (which require a DOCTYPE) but nonetheless stay in quirks mode. So IE6 will render pages in quirks mode if the DOCTYPE is preceded by an XML prolog, regardless of whether a full DOCTYPE is stated or not. Any XHTML page beginning with the following code would be rendered in quirks mode by IE6 (which can be useful if you need to trigger quirks mode in IE6 but no other browsers):

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

In IE7, the XML prolog is simply ignored. However, for compatibility with older web browsers, the W3C Consortium suggests that authors of XHTML documents could consider omitting the XML declaration.

And again, a web page that does not include a DOCTYPE at all will render in quirks mode. Take note also that quirks mode in any version of IE prior to IE7 will also be triggered if anything but whitespace precedes the DOCTYPE. For example if a comment or any tag appears before the DOCTYPE, IE will render in quirks mode:

<!-- This comment will put IE 6, 7 and 8 in quirks mode -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">