←back to Articles

Writing Valid XHTML 1.1

This content is 16 years old and may not reflect reality today nor the author’s current opinion. Please keep its age in mind as you read it.

There’s a lot of good reasons to write valid XHTML (even if the vast majority of sites don’t bother):

  • Your site will render better, faster and more consistently across all browsers.
  • Your layout will be pushed from tables and tags to CSS, separating data from presentation and reducing maintenance costs.
  • Computers (most notably, search engines) will be able to parse and make sense of your content easier than they might otherwise have been able to.
  • You’re supporting standards compliance (which translates to freedom for you and your users) and you can advertise valid XHTML using the W3C logos:

Once you’ve gone to the effort of writing valid XHTML and CSS and the W3C Markup Validation Service (http://validator.w3.org/) is happy with your efforts you’ll still want to make sure you’re serving your content with the right mime-type: application/xhtml+xml, but only to browsers that support it (and ask for it via the HTTP Accept: header)… most notably not IE6 😐

It’s unfortunate those who care about standards compliance have to jump through hoops by implementing content negotiation, but it’s not too hard to do…. for example in PHP you can do something like this:

  header("Vary: Accept");
  if (stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml") || stristr($_SERVER["HTTP_USER_AGENT"], "W3C_Validator"))
    header("Content-Type: application/xhtml+xml; charset=utf-8");
     header("Content-Type: text/html; charset=utf-8");

Notice that the validator won’t send an Accept header by default. You can force it to, but I’m just checking for the user agent; if you don’t you’ll get a warning about the mime-type even if the document is valid (and you’re serving it correctly).

Note also that neither this blog nor the Australian Online Solutions blog are valid XHTML (and I’m not rewriting Blogger templates to make them compliant), but TrustSaaS.com is and validates cleanly.