Sign in? or Create account?

Articles

Introduction to XHTML

by Mr. Matt Milburn

 

We are all resistant to change at times, especially once we've already grown comfortable with our own way of doing things. But as web developers, it is our responsibility to stay up-to-date with current web standards and other internet trends. We need to frequently make changes to the way we create websites in order to provide accessible, compatible, and overall great looking websites to our audience. HTML made the jump to XHTML quite a while back but there are still some of us out there that are resistant to the change. Those hesitant about making the switch would be happy to hear that XHTML isn't too different from regular HTML and doesn't take long to learn.

 

What is XHTML and why should I use it?

Compatibility is the main reason to use XHTML. Not every browser on the market has the capabilities to reading "bad" HTML, especially those built into hand-held devices such as cell phones. Extensible Hyper Text Markup Language (XHTML) allows for easier document processing with the convenience of standard XML tools, thus making our web pages far more accessible to a variety of different browsers and devices.

XHTML will soon replace HTML. If we continue to create web pages in regular HTML, we may find that our web pages won't behave properly or consistently across various browsers and devices. So, the sooner you can make the migration from HTML to XHTML, the easier things will be for you later on!

What's are some of the differences?

The main difference from HTML is that XHTML is an application of XML (Extensible Markup Language), while HTML is an application of SGML (Standard Generalized Markup Language). In contrast, one is more forgiving than the other. XHTML, the less forgiving language, demands to be well-formed, so there are more rules and less exceptions than there are in HTML. Overall, the differences are pretty black and white. Let's look at some of the current differences between HTML and XHTML:

HTML 4.01XHTML
Case-insensitiveTags and attributes must be all lowercase
Forgives non-closed tagsAll tags MUST be closed
Forgives improper nestingAll tags MUST be properly nested
Allows attribute minimizationAll attributes MUST be a name value pair (name="value")
Allows unquoted attributesAll attributes MUST be quoted
Requires no DOCTYPEMUST have a DOCTYPE (explained later)
Allows non-encoded entitiesRequires entities to be encoded

Examples:

Incorrect:
<P>The quick brown fox <B>jumps</B> over the lazy dog.
<UL>
	<LI>He ran...
	<LI>& ran...
	<LI>& ran!
</UL>
<br>
<hr noshade>
<P ALIGN=center>The end.
Correct:
<p>The quick brown fox <b>jumps</b> over the lazy dog.</p>
<ul>
	<li>He ran...</li>
	<li>&amp; ran...</li>
	<li>&amp; ran!</li>
</ul>
<br />
<hr noshade="noshade" />
<p align="center">The end.</p>

We also need to make sure that we don't nest list elements (or any other element for that matter) improperly, which is a common mistake in lists. Notice the correct way to nest a list inside of another list is to place your nested list inside of a list item, not after it.

Incorrect:
<ul>
	<li>Mammal</li>
		<ul>
			<li>Dog</li>
			<li>Cat</li>
		</ul>
	<li>Reptile</li>
	<li>Amphibian</li>
</ul>
Correct:
<ul>
	<li>Mammal
		<ul>
			<li>Dog</li>
			<li>Cat</li>
		</ul>
	</li>
	<li>Reptile</li>
	<li>Amphibian</li>
</ul>

 

Deprecated Tags & Attributes:

On top of a more strict syntax, some tags and attributes have now become deprecated, or in other words, their use is now being discouraged. The center tag was once used to center elements, we used the font tag to format text, and the bgcolor attribute let us change the background color of elements. I'm afraid that they are done for.

The functionality these tags and attributes once provided is not lost however. Some tags and/or attributes simply have a direct replacement, but for the most part, the use of CSS (Cascading Style Sheets) through style sheets and inline styles (use of the core style attribute) will give us what we want. CSS will allow you to apply styles to any element in your document to give you the effect that these once useful tags provided. Below is a table that will show you some deprecated tags and their alternative.

TAGUSEALTERNATIVE
<font>font & misc. styling<p style="font-size:12px;">Hello World</p>
<s>
<strike>
strike-through<p style="text-decoration: line-through;">Hello World</p>
<u>underline<p style="text-decoration: underline;">Hello World</p>
<center>center an element<p style="text-align: center;">Hello World</p>
ATTRIBUTEUSEALTERNATIVE
bgcolordefine background color<p style="background: #000;">Hello World</p>
hspacehorizontal margin of image<p style="margin: 0px 10px 0px 10px;">Hello World</p>
vspacevertical margin of image<p style="margin: 10px 0px 10px 0px;">Hello World</p>
linkdefine hyperlink colora:link {color: #00C}
alinkdefine active hyperlink colora:active {color: #000}
vlinkdefine visited hyperlink colora:visited {color: #C0C}

It should also be made a good practice to avoid underlining text for emphasis. Underlines have widely become associated with hyperlinks and people expect underlined text to act as a hyperlink. So to avoid confusion, it is encouraged that underlining be preserved for hyperlinks only.

Click here, for an always up-to-date list of deprecated tags provided by www.w3schools.com.

 

Declare a DOCTYPE:

The DOCTYPE tag associates the document we are using with a particular Document Type Definition (DTD) which specifies the syntax used in your document. Your DOCTYPE is declared on the first line before anything else in your document and is the only tag that does not get closed. Let's look at the three DOCTYPEs used in XHTML.

Strict (recommended)

This DOCTYPE does not support deprecated tags and requires well-formed code. If you're a beginner, consider it a goal to eventually use this DOCTYPE in all of your web pages.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict/EN" 
"http://www.w3.org/TR/xhtmll/DTD/xhtmll-strict.dtd">
Transitional (most common)

This DOCTYPE allows deprecated tags. This is the most commonly used DOCTYPE, but the "Strict" DOCTYPE is still more desirable since it doesn't allow bad HTML.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
Frameset

If you're using frames and require something that will support Frameset, you'll want to go with this DOCTYPE.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" 
"http://www.w3.org/TR/xhtmll/DTD/xhtmll-frameset.dtd">

 

So there you have it! You are now ready to start using XHTML in your web pages. Pretty easy, right? One last thing that you will definitely want to bookmark or add to your favorites is the XHTML Validator provided by www.w3.org. This validator will check your syntax and tell if you made any mistakes that are incorrect under the DOCTYPE that you used.

For more information on XHTML visit www.w3schools.com/xhtml.

Back to Top Back to Top

 

About the author

Mr. Matt Milburn

Mr. Matt Milburn (USA)

Mr. Milburn is an experienced web developer and the co-founder of the graphic design and web development company, Wave Motion Studios. While self-taught in the area of web development, Matt has studied graphic design at Oklahoma State University and the University of Central Oklahoma. Matt has worked as a web programmer for the Administration of UCO for four years. Throughout his career, Matt has gained much experience working on several unique, web-related projects for various clients in the Oklahoma City and Dallas areas.

 

© 2007-2010 Samenmais Corporation™