Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Related Articles

Creating XHTML Compliant Code

This article contains a few simple pointers to make sure all your HTML code is XHTML compliant. This will be an evolving document as there will periodically be new ideas and methods of doing things.

It’s amazing that despite how simple and fundamental all of these things are, very few people coding HTML get them right.

  • Code will be well-formed and be easier maintained, as certain aspects will always be consistent.
  • More chance of your document being correct. If your HTML isn’t well-formed, sometimes browsers have to make a guess as to how it should be rendered. It may work fine now, but maybe browsers will change how they guess.
  • When combined with using valid CSS, your documents will generally load a lot quicker, and render properly on a wide variety of browsers.
  • Goes a long way to improving accessibility (although don’t make the mistake of thinking your pages are accessible just because they are valid XHTML).

There are three different document types you can use for XHTML. You must specify which kind of document right at the top.

  • XHTML 1.0 Strict – This is a well-formed XHTML document that uses no deprecated tags such as basefont, center, font, s, strike and u. It also does not allow deprecated attributes such as bgcolor, align, valign. A fairly comprehensive list can be found at
  • XHTML 1.0 Transitional – This is by far the most widely used document type. It allows the use of the deprecated tags listed above. It doesn’t allow usage of frames.
  • XHTML 1.0 Frameset – This is the same as the transitional document, but allows frames.

It is recommended that you use XHTML 1.0 Strict whenever possible.

To use XHTML 1.0 Strict, place the following code at the very top of your document:

Listing 1 listing-1.html
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

To use XHTML 1.0 Transitional, place the following code at the very top of your document:

Listing 2 listing-2.html
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

To use XHTML 1.0 Frameset, place the following code at the very top of your document:

Listing 3 listing-3.html
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"

This one is fairly simple – make sure all tag names and attribute names are in lower case. XML is case-sensitive, which means <BODY> is different to <body>.

The same applies to all attributes within tags.

Listing 4 listing-4.html
<!-- Invalid: -->
<DIV class="foo"></div>
<div CLASS="foo"></div>
<DIV CLASS="foo"></DIV>
<BR />
<!-- Valid: -->
<div class="foo"></div>
<br />

You must put quotes around all attribute values, even if it seems like they don’t need them. Personally, I recommend you use only double quotes ("), but single quotes are also valid. It’s best to be consistent.

Listing 5 listing-5.html
<!-- Invalid: -->
<div class=foo></div>
<!-- Valid: -->
<div class="foo"></div> (recommended)
<div class='foo'></div> (not recommended)

All tags that have been opened must be closed. Sometimes tags won’t contain any data in between and opening and closing tag, and can be shortened by ending the element with a slash.

Listing 6 listing-6.html
<!-- Line breaks: -->
<br> is invalid
<br /> is valid (recommended)
<br></br> is valid (not recommended)
<!-- Images: -->
<img src="..."> is invalid
<img src="..." /> is valid (recommended)
<img src="..."></img> is valid (not recommended)
<!-- Paragraphs: -->
<p>This is an invalid paragraph
<p>This is a valid paragraph</p>

Finally, tags should not overlap each other – they should be nested.

Listing 7 listing-7.html
<!-- Invalid: -->
<p>This is some <em>invalid HTML code</p></em>.
<!-- Valid: -->
<p>This is some <em>valid HTML code</em></p>.

Most people won’t even bother with this one, but you will need to if you want your document to validate.

If you want to show a drop-down select box, with a certain item selected, most people use code like this:

Listing 8 listing-8.html
<select name="my_select">
    <option value="1">Item 1</option>
    <option value="2" selected>Item 2</option>

Notice the second item is selected. The way this has been indicated is invalid, as there needs to be a value with the selected attribute. It should in fact look like:

Listing 9 listing-9.html
<select name="my_select">
    <option value="1">Item 1</option>
    <option value="2" selected="selected">Item 2</option>

There are a number of tags this will apply to, such as checkboxes and radio buttons.

Often you will need to pass multiple parameters in a link. This means each parameter will be separated using an ampersand (&). If you are hard-coding these links, remember to change the & to &amp;. If you don’t do this, your document won’t validate.

Listing 10 listing-10.html
<!-- Invalid: -->
<a href="">My invalid example link</a>
<!-- Valid: -->
<a href=";b=value2">My valid example link</a>

If you include JavaScript directly in your document (as opposed to using an external file), chances are that the code won’t validate. To get around this, you need to escape the JavaScript code with the <![CDATA[ ... ]]> tags.

Listing 11 listing-11.html
<!-- Invalid: -->
<script type="text/javascript">
    function min(a, b)
        return a < b ? a : b;
<!-- Valid: -->
<script type="text/javascript">
    function max(a, b)
        return a > b ? a : b;

Of course, including external JavaScript files is the preferred method.

Listing 12 listing-12.html
<script type="text/javascript" src="min.js"></script>
<script type="text/javascript" src="max.js"></script>

You should always validate your document! Do it when you’ve developed the initial template, do it when you make some changes to the template, and do it again when you release the site!

It’s really easy for things to accidentally break when you’re splitting files up to use in your scripts or templating engine.

The validator is at, and will generally identify all problems with your documents. You can even validate this page.

  1. W3C XHTML Recommendation
  2. W3C XHTML Validator
  3. list of deprecated HTML tags and attributes

Other Options

Creating XHTML Compliant Code