Introduction
Although many people have never heard of it, XHTML is really the future of the internet. It is the newest generation of HTML (coming after HTML 4) but has many new features which mean that it is, in some ways, like XML. In this tutorial I will explain how XHTML differs from HTML and how you can update your pages to support it.
Note: It is necessary to already have a basic understanding of HTML before reading this tutorial as it deals with the differences between XHTML and HTML.
What is XHTML
XHTML stands for eXtensable HyperText Markup Language and is a cross between HTML and XML. XHTML was created for two main reasons:
- To create a stricter standard for making web pages, reducing incompatibilities between browsers
- To create a standard that can be used on a variety of different devices without changes
XHTML is a web standard which has been agreed by the W3C and, as it is backwards compatible, you can start using it in your webpages now. Also, even if you don't think its really necessary to update to XHTML yet, there are three very good reasons to do so:
- It will help you to create better formatted code on your site
- It will make your site more accessible (both in the future and now due to the fact that it will also mean you have correct HTML and most browsers will show your page better)
- XHTML is planned to replace HTML 4 in the future
The Main Changes
There are several main changes in XHTML from HTML:
- All tags must be in lower case
- All documents must have a doctype
- All documents must be properly formed
- All tags must be closed
- All attributes must be added properly
- The name attribute has changed
- Attributes cannot be shortened
- All tags must be properly nested
The Doctype
The first change which will appear on your page is the Doctype. When using HTML it is considered good practice to add a Doctype to the beginning of the page like this.
Although this was optional in HTML, XHTML requires you to add a Doctype. There are three available for use.
Strict - This is used mainly when the markup is very clean and there is no 'extra' markup to aid the presentation of the document. This is best used if you are using Cascading Style Sheets for presentation.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w
3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
Document Formation
After the Doctype line, the actual XHTML content can be placed. As with HTML, XHTML has <html> <head> <title> and <body> tags but, unlike with HTML, they must all be included in a valid XHTML document. The correct setup of your file is as follows:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Page Title</title>
OTHER HEAD DATA
</head>
<body>
CONTENT
</body>
</html>
Part 2 - XHTML Tags
Introduction
One of the major changes to HTML which was introduced to XHTML is that tags must always be properly formed. With the old HTML specification you could be very sloppy in your coding, with missing tags and incorrect formation without many problems but in XHTML this is very important.
Lower Case
Probably the biggest changes in XHTML is that now not only the tags you use but, the way in which you write them must be correct. Luckily the major change can be easily implemented into a normal HTML document without much problem.
In XHTML, tags must always be lower case. This means that:
Code: Select all
<FONT>
<Font>
<FoNT>
Code: Select all
<font>
Edit -> Preferences -> Code Format
and making sure that Case For Tags is set to:
Code: Select all
<lowercase>
Code: Select all
lowercase="value"
The second change to the HTML tags in XHTML is that they must all be properly nested. This means that if you have multiple tags applying to something on your page you must make sure you open and close them in the correct order. For example if you have some bold red text in a paragraph, the correct nesting would be one of the following:
Code: Select all
<p><b><font color="#FF0000">Your Text</font></b></p>
<b><p><font color="#FF0000">Your Text</font></p></b>
<p><font color="#FF0000"><b>Your Text<b></font></p>
Code: Select all
<p><b><font color="#FF0000">Your Text</p></font></b>
Closing Tags
The previous two changes to HTML should not be a particular problem to you if your HTML code is already well formed. The final change to HTML tags probably will require quite a lot of changes to your HTML documents to make them XHTML compliant.
All tags in XHTML must be closed. Most tags in HTML are already closed (for example <p></p>, <font></font>, <b></b>) but there are several which are standalone tags which do not get closed. The main three are:
Code: Select all
<br>
<img>
<hr>
Code: Select all
<br></br>
<img></img>
<hr></hr>
Code: Select all
<br />
<img />
<hr />
In case you are wonder
ing how the <img> tag works if it has all the normal attributes included, here is an example:
Code: Select all
<img src="myimage.gif" alt="My Image" width="400" height="300" />
Part 3 - Attributes
Introduction
In this part of the XHTML tutorial, I will show you the changes to HTML attributes in XHTML. HTML attributes are the extra parts you can add onto tags (such as src in the img tag) to change the way in which they are shown. There are four changes to the way in which attributes are changed.
Lowercase
As with XHTML tags, the attributes for them must be in lowercase. This means that, although in the past, code like:
Code: Select all
<table Width="100%">
Code: Select all
<table width="100%">
Correct Quotation
Another change in the HTML syntax is that all attributes in XHTML must be quoted. In HTML you could have used the following:
Code: Select all
<table width=100%>
Code: Select all
<table width="100%">
It has become common practice in HTML to shorten a few of the attributes to save on typing and on transfer times. This method has become almost a standard. As with other common practices in HTML, this has been removed from the XHTML specification as it causes incompatibilities between browsers and other devices.
An example of a commonly shortened tag is:
Code: Select all
<input type="checkbox" value="yes" name="agree" checked>
Code: Select all
checked
Code: Select all
checked="checked"
Code: Select all
<input type="checkbox" value="yes" name="agree" checked="checked">
The ID Attribute
Probably the biggest change from HTML to XHTML is the one tag attribute change. All other differences have been just making tags more compatible. This is the only full change.
In HTML, the <img> tag has an attribute 'name'. This is usually used to refer to the image in javascript for doing actions like image rollovers. This attribute has now been changed to the 'id' attribute. So, the HTML code:
Code: Select all
<img src="myimage.gif" name="my_image">
Code: Select all
<img src="myimage.gif" id="my_image" />
Code: Select all
<img src="myimage.gif" id="my_image" name="my_image" />
This tutorial has shown you most of the changes between HTML and XHTML. As you will have seen, there are actually very few changes and, the next time you update your site, I recommend changing your code to make it XHTML compatible. It will not only make your site 'future-proof' but will also mean that you will have more correct code and should have fewer browser incompatibility problems.