To be XHTML or to not be XHTML - that is the question?

When I first started using the XHTML1.0 Strict doctype, about three years ago, I really wasn't sure why I was using it. Perhaps my decision had something to do with the fact that it seemed to be the "in thing"! After reading more on the subject, I decided that I had made the right decision. I will explain first, why I chose to use XHTML, then why I chose to use the strict doctype.

Before I start, just a brief introduction to XHTML.

What is XHTML

XHTML (eXtensible HyperText Markup Language) was created by the W3C as a replacement for HTML. It is a subset of XML (eXtensible Markup Language), which basically means, when you write XHTML, you are actually using XML to create a group of tags and attributes, following the rules of XML.

XHTML code must follow a set of rules which are considerably stricter than those imposed in HTML. For instance, XHML specifies that all documents must be well-formed and elements must be lowercase. If you do not do this, your page won't validate and may not display correctly.

Why use XHTML over HTML?

The key differences between XHTML and HTML are:

Over all, given its stricter rules, XHTML appeared to be a cleaner approach to writing code. Documents now had to be well-formed, so no longer were elements allowed to be left open when they should be closed, or attributes written without double or single quotes. XHTML gave order to the page. It abolished sloppy coding. The W3C couldn't have put it better:

The XHTML family is the next step in the evolution of the Internet. By migrating to XHTML today, content developers can enter the XML world with all of its attendant benefits, while still remaining confident in their content's backward and future compatibility - W3C.

The Strict doctype

I chose to use the strict doctype because it seemed the right direction to move in. Strict, like the name suggests, places stricter rules on how you write your code. And I liked that. It was as if XHTML made sure things were done properly.

This all sounds good so far, until we introduce a little thing called "media type".

Not all is as it seems...

The media type for "true XHTML pages", i.e., pages that are parsed like XML documents, is application/xhtml+xml...but here's the snag: Internet Explorer doesn't understand application/xhtml+xml, and will render the page incorrectly if you try to use it. Most people, who still want to use XHTML, set their media type to text/html (which is how I do it), but there's still a small problem. The W3C say that:

...in general, this media type [text/html] is NOT suitable for XHTML.

Yes, that's right, with a big, old capitalised NOT. So, why do so many XHTML coders use it? Well, because there's not a lot else to do, apart from dropping down a level to HTML 4. But what happens to those of us who want to write XHTML webpages and have them work for all browsers? Well, we're not at a loss yet, as the W3C also say:

[XHTML1] defines a profile of use of XHTML which is compatible with HTML 4.01 and which may also be labeled as text/html.

So, let me get this straight, our browsers won't break if we use text/html? Yep, that's right. The page will be displayed as if it were plain old HTML (for a list of websites, big and small that use the XHTML 1.0, doctype see below). It seemed that the W3C had changed their mind. At one point, they thought it was wrong to use text/html with XHTML pages, then afterwards, they decided that it was OK, for they also say:

It is intended to be used as a language for content that is both XML-conforming and, if some simple guidelines are followed, operates in HTML 4 conforming user agents.

So basically you can use text/html with XHTML documents, just the proper way to do it is to use application/xhtml+xml. Just in case you were interested, the W3C's website is actually in XHTML 1.0 Transitional.

So, why use XHTML 1.0 then? Why not stick to HTML?

After gaining more background on the inner-workings of XHTML, I moved on to a question that had been at the back of my mind for a while, and one I felt I could now answer adequately: why use XHTML at all, if the media type you are supposed to use with it, is not fully implemented (i.e., if Explorer doesn't support it)? I know the W3C say it's OKish, but I wanted a deeper answer...In order to get to the bottom of this, I decided to go about it from a different angle.

Why not use XHTML 1.0? Here's the conclusion I came to: XHTML 1.0 (particularly strict) forces you to write standardised, well-formed code - and surely there can't be anything wrong with that? In my opinion, until Explorer supports XHTML, it's not so bad that you don't use the correct media type, because you're doing all you can to ensure your code is clutter-free and that it won't cause your site to "break" or render incorrectly. OK, you can validate HTML 4, too, but here's my argument: XHTML is the future of the web, or as the W3C put it - the XHTML family is the next step in the evolution of the Internet. It is more demanding, thus produces cleaner results, than HTML 4, and doesn't allow you to write haphazard code. I would rather be an advocate of XHTML and use it as much as possible, than take a step downward and use HTML 4. One day Explorer will support XHTML, and all those people who wrote their pages in HTML 4 will have to rewrite them.

Microsoft at it again...

Many people use HTML 4 because Explorer doesn't support XHTML. In my opinion, that isn't a good enough reason to discard XHTML. Explorer doesn't support CSS3 (though version 7 has introduced some new CSS2 properties that weren't in the earlier versions). I might be a little more sympathetic if Microsoft weren't trying to dominate the marker and create their own standards. OK, I'll stop before this becomes a Microsoft rant...Anyway, getting back to the topic at hand...

XHTML 2

XHTML 2 probably won't be backward compatible with XHTML 1.0, and there will no doubt be some big differences between XHTML 1 and XHTML 2 (kind of like the differences between ActionScript 2 and 3) but it doesn't change the fact that you are still writing XHTML - you're taking a step in the right direction; preparing yourself for the time when XHTML 2 is just about as common as sliced bread :p. That time may be a way off, but it will happen.

Content Negotiation

For those of you that aren't satisfied with what I've discussed above, and want to use application/xhtml+xml as your mime type, there is still another rout: using conditional statements to serve your pages out as application/xhtml+xml to browsers that support it. This process is known as content negotiation and involves using a server-side language, like PHP, to detect which browser is being used and change the media type appropriately. I have never used content negotiation myself, so feel it wouldn't be practical for me to post links on how to do it here, in case they are wrong. If you would like to use it, I would recommend doing a search for content negotiation and your preferred programming language.

Why is there not a bigger support for the application/xhtml+xml media type?

It's mainly to do with Explorer not supporting XHTML. Why do they not? Microsoft have always been rather vague about the issue.

Conclusion

In this article, I have tried to list some of the reasons why I use XHTML and why I believe XHTML is better than HTML. At the end of the day, it is your choice. Use whatever technology you feel comfortable with. There are plenty of people who believe XHTML should be used instead of HTML, and plenty who do not.

Ever since I've started coding in XHTML I have found it to be a lot more satisfying than HTML 4 - that's my honest opinion. Sure, some will bash XHTML and say what is the point of using it when it isn't supported by the main browser on the planet? Well, my answer is that there's more to life than Explorer. I am fully aware that many people use Internet Explorer, but I believe that the benefits of using XHTML are such that it should not be cast aside in favour of backward compatibility. I always make sure that my websites work in older browsers, but within reason. You have to draw the line somewhere, otherwise things just become ridiculous.

So, if you fancy ditching that tag soup, why not give XHTML a go? Or even try going Strict...You might like it.

Some sites that use XHTML

XHTML 1.0 Strict delivered as text/html

XHTML 1.0 Transitional delivered as text/html

As you can see, not just specialist sites use XHTML, but big corporations like BT, Vodafone, Sky, Network Rail, HSBC and Yell. Then there's the video games sites, IGN, Gamespot, Tomb Raider Forums, GameFAQs, the pc-related sites, PHP, MySQL, MyEclipse, Apache, Linux. Wikipedia use it, as do Facebook...even Greenpeace uses XHTML!

References

Got any questions or comments? Feel free to mail me @ helen@alternategateways.com.

Back to resources.