XHTML Doctypes

XHTML Doctype is a very important part of every XHTML document. When correct Doctype is set, web browser knows how to parse your website and how to correctly display it to the user.

Unfortunately, Doctype definition is quite difficult to remember and we always need to google for the cheat sheet, so here is another one:

XHTML 1.0 Strict:

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

XHTML 1.0 Transitional:

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

XHTML 1.0 Frameset:

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

That's it! Remember any of the above lines should be placed as the very first line of every HTML Document.

If you are looking for doctypes for other web-documents visit this W3C page.

Which Doctype should I choose?

Frameset is reserved for pages with frames, so most of the times the real choice is only Strict vs Transitional. I always go with Transitional, mainly because this Doctype allows to use more HTML tags and attributes. Table below shows what tags and attributes are allowed for both Doctypes:

HTML tagStrict DoctypeTransitional Doctype
<center>NOYES
<font>NOYES
<iframe>NOYES
<strike>NOYES
<u>NOYES
HTML attributeStrict DoctypeTransitional Doctype
alignNO (only allowed on: <col>, <colgroup>, <tbody>, <td>, <tfoot>, <th>, <thead>, <tr>) YES
alinkNOYES
backgroundNOYES
bgcolorNOYES
borderNO (only allowed on <table> element)YES
heightNO (only allowed on <img> and <object>YES
hspaceNOYES
languageNOYES
linkNOYES
nameNO (allowed in HTML 4.01 Strict, but now allowed on <form> and <img> in XHTML 1.0 StrictYES
noshadeNOYES
nowrapNOYES
targetNOYES
vlinkNOYES
vspaceNOYES
widthNO (only allowed on: <img>, <object>, <table>, <col>, <colgrup>)YES
Did you like my article? Share it!
Share/Bookmark

Fun facts

I find DOCTYPEs very interesting (sad but true). In particular, I've always wondered what practical effect they have on browsers - since browsers will do their very best to render even horribly broken HTML, and implement their own deviations from W3C, will an incorrect DOCTYPE have any effect? And since IE has such an abstract understanding of what makes a healthy HTML document, how does it interpret dtds, if at all?

There is a fun fact about IE's behaviour as regards doctypes. Sure you can use an XML doctype before the XHTML doctype to force quirks mode in older IEs, or use a malformed doctype (or none at all). But get this. IE7+ will attempt to download the dtd from the location specified in the DOCTYPE every time. It doesn't even cache it, or actually do anything useful with it (god knows it doesn't respect it). This means that every time anybody using IE7+ loads any HTML page with a doctype, W3.org gets an HTTP request! But this is the punchline: W3's hosted dtds have some browser-sniffing going on on the back end for each incoming HTTP request - if it's a normal, healthy looking one it will simply HTTP respond with the .dtd file. But if it detects IE, it returns a single word: 'NO'. I think this is one of the most hilarious IE deficiencies ever.

A nice way to avoid that stupid (but inconsequential) behaviour and keep everyone happy is to use the HTML5 doctype. It's valid on pretty much any well-formed XHTML document, forward-compatible, produces correct behaviour in all modern browsers, and easier to remember too:

<!DOCTYPE html>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <csharp>, <css>, <html>, <javascript>, <vb>, <vbnet>, <xml>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.