XML technologies / CSS / Advanced websites with CSS / Validation and backward compatibility / Using DOCTYPE switching

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

Pages for old browsers can look terrible or disappear in newer ones: once-tolerated messy HTML fails stricter standards. Conversely, modern pages can break or disappear in old browsers.

IE 5 for Macintosh, IE 6 for Windows, Firefox and Safari support DOCTYPE switching. They adapt rendering to the document type declared by the DOCTYPE declaration at the page’s start.

The declaration DOCTYPE has always belonged to HTML and was recommended, but developers ignored it and browsers did not support it. Retrofitting pages can be substantial work, eased by good editing or design tools.

Reading the document’s DOCTYPE selects strict standards mode or forgiving quirks mode. Strict mode follows the declared HTML standard closely; quirks mode is more tolerant.

Use this for new pages and add declarations DOCTYPE to old ones. Embedded or external stylesheets should use standards mode, as in this HTML 4.0 example:

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

The corresponding declaration DOCTYPE for the newer standard XHTML 1.0 is:

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

If pages do not support CSS and use deprecated HTML instead, this declaration DOCTYPE enables quirks mode so modern browsers display them correctly:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

For XHTML 1.0, use:

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

The second part of these last declarations DOCTYPE specifies the permissive version, enabling quirks mode without strict HTML compliance.

If a valid document differs across modern browsers, first check strict mode. IE needs special syntax for the declaration DOCTYPE to enter quirks: choose HTML 4.0 Transitional and omit the URL:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

Any URL in the DOCTYPE forces IE into standards mode without examining the definition. XHTML declarations do likewise. For quirks, use the declaration DOCTYPE above.

Without any DOCTYPE-declaration, most browsers including IE 6 use quirks. HTML and XHTML require DOCTYPE in every document, so include it.

This book uses pure standards-compliant XHTML 1.0 except old-browser examples. Every example therefore begins alike:

<!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">
  <head>
    <title>Hier kommt der Titel der Seite hin.</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

The declaration DOCTYPE on the first line ensures standards mode in newer browsers.

IE’s XML DOCTYPE-switching bug
XML purists may wonder why the usual XML version declaration is missing:

<?xml version="1.0" encoding="iso-8859-1"?>

XML specifies an opening declaration <?xml ... ?> followed by the declaration DOCTYPE.

Unfortunately IE 6 mishandles it. A declaration <?xml ... ?> or anything else, even whitespace, before the declaration DOCTYPE makes it ignore the declaration DOCTYPE and use quirks mode. Omit the XML version declaration for the best cross-browser CSS support.

Fortunately XML permits omission without special problems; XHTML generally works correctly.

  

 

 

 

back to top