Welcome to Code Couch

How do I choose a Doctype?

Posted by at 7:55am on December 20, 2005.

As the W3C reminds us, there are many types of HTML in use on the internet. To ensure these documents are displayed correctly, the various programs (browsers, mobile phones, PDAs etc) that view these documents need to know what kind of document structure they contain. These programs look to the Document Type Definition (referred to as a DTD and commonly called a doctype).

The doctype appears as the very first thing on the page that is delivered to the user, allowing the browser agent to make decisions about how to display the page. Whilst a document can still display within a web browser in the absence of a doctype, defining one implies the document is “well formed” and allows a browser to make assumptions that assist it in rendering correctly. It also prevents the browser from making the wrong assumptions and displaying the document in an unexpected “quirks” mode.

So how do you decide what doctype to use? Again, the W3C have provided a list of recommended DTDs for your document but they don’t offer a lot of guidance to the developer in choosing one. In my opinion it comes down to a question of whether you are creating an HTML or an XHTML document.

Choosing between HTML and XHTML

Unless you are going to be serving your documents as “text/xml” and loading them into an XML parser to allow your XML tools to manipulate the page, then there is no reason to develop your documents as XHTML. A majority of web pages on the internet are HTML – and this should be the default choice for developing your documents – especially if your web host is serving your pages as “text/html”.

Frameset, Transitional or Strict?

If you are developing frameset documents, then the choice is already made for you – you will use a Frameset doctype. The rest of the time you should consider using a Strict doctype which is almost the same as the Transitional doctype, but with the presentation specific elements removed (deprecated).

The big difference between Transitional and Strict doctypes is the seperation of the presentation and content layers within the document. The idea is that you use CSS to define the layout of your document, and rely on well formed HTML to define the content of the document. This ensures that the different programs that may be used to view your document are able to read the content consistently, and apply the appropriate presentation layer (via CSS).

When should you use a Transitional doctype?

The Transitional doctype is appropriate if you are working on an existing HTML document that contains presentational elements (like the <center>, and <font> tags). These tags were totally acceptable for the older HTML 3.2 definition (where the presentation and content layers were freely combined) and many sites still contain documents that have not been “upgraded” to use the most current standards.

If you are starting a new site, then you should be using a Strict doctype rather than a Transitional one.

So tell me what to do!

To summarise, I would suggest that you use the following doctype for each of the following “common” development scenarios:

  • Frameset HTML 4.01 – when maintaining or updating a frameset HTML document
  • Transitional HTML 4.01 – when maintaining or updating an old HTML document that contains deprecated tags
  • Strict HTML 4.01 – when building a new site which seperates the presentation and content layers
  • Frameset XHTML 1.0 – when maintaining or updating an HTML frameset XHTML document
  • Transitional XHTML 1.0 – when building or updating a site that needs to support HTML presentation layer tags
  • Strict XHTML – when building a new site that will be manipulated by an XML parser and served as “text/xml”

The decision of whether to use Strict XHTML 1.0 or XHTML 1.1 is left to the reader. If you are developing Strict XHTML documents and serving them to a wide audience, you should have already made this decision based on the audience and the technology being used to serve (and read/parse) the documents.

Post to Twitter

Tags: , ,

Comments

There are no responses to this post.

Leave a reply

Comments are closed for this post.