I'm sure you've heard people mumbling about XHTML and how it combines HTML and XML to create the "next generation" of HTML. I too had heard the mumbling, but actually had no idea what XHTML was... up until about 2 months ago. In this article, Mitchell gives us a quick run down of what XHTML is, some of its benefits, how it can be used, and what it looks like. He wraps up the article with a couple of XHTML examples.
XHTML is HTML "reformulated" to conform to the current Extensible Markup Language (XML) standard, version 1.0. Imagine taking the best parts from the HTML language and mixing them with all of the great aspects of XMLÖ then youíre coming close to imagining the power and flexibility of XHTML.
XHTML has much stricter language syntax that HTML, however. To create fully valid XHTML documents, they must adhere to these rules/guidelines:
All tags must be closed
With normal HTML documents, some browsers will still render the contents of a <table> even if you donít close the table with a </table> tag. This allows developers to become lazy and forgetful. The tags within an XHTML document must always be nested correctly and closed properly.
If we have the following HTML 4.0 compliant table:
<p><b>Welcome to my page
... you can see straight away that the <p>, <b>, and <hr> tags arenít closed. This is a big no-no for XHTML documents and will raise a parser error, because all tags must be closed (yes, even the <p> tag).
The XHTML 1.0 compliant version of the table shown above looks like this:
<p><b>Welcome to my page</b></p>
Notice how the <p>, <b>, and <hr> tags are now closed? To close tags like <hr>, we can simply add a space and forward-slash within the tag, like this: <hr />.
Attributes must contain quoted values
All tag attributes, such as <p align="center"> must be enclosed within double quotes. You no longer have the choice of either single or double quotes. Also, for attributes which have no value, or arenít quoted such as
... you must assign a value to that attribute (even though it wont be used), and surround it in double quotes, for example:
All element and attribute names must also be lower case.
Be careful with special characters
Because of the way XHTML documents are validated and must conform to specific rules, HTML comments like this:
<!-- This is a comment -->
If you're using other HTML characters such as <, > and & in attribute values, for example, then they should be replaced with their corresponding HTML entity representations such as "<", ">" and "&" respectively.
Last but not least, instead of giving tags such as <input>, <select>, and <form> a value for the "name" attribute, you should use the "id" attribute instead. In XHTML documents, the "name" attribute is rendered useless, and belongs back with HTML 4.0.