XHTML: Differences from HTML

Differences from HTML

Document Structure

//this example shows an XHTML document with a minimum of required tags

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

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Title of document</title>
</head>

<body>
  some content 
</body>

</html>
  • XHTML DOCTYPE is mandatory.
  • The xmlns attribute in <html> is mandatory.
  • <html>, <head>, <title>, and <body> are mandatory.

XHTML Elements

In HTML, some elements can be improperly nested within each other, like this:

<b><i>This text is bold and italic</b></i>
In XHTML, all elements must be properly nested within each other, like this:

<b><i>This text is bold and italic</i></b>
//XHTML elements must always be closed

//this is wrong
<p>This is a paragraph
<p>This is another paragraph

//this is correct
<p>This is a paragraph</p>
<p>This is another paragraph</p>
//empty elements must also be closed
//this is wrong
A break: <br>
A horizontal rule: <hr>
An image: <img src="happy.gif" alt="Happy face">

//this is correct
A break: <br />
A horizontal rule: <hr />
An image: <img src="happy.gif" alt="Happy face" />
//XHTML elements must be in lower case
//this is wrong
<BODY>
<P>This is a paragraph</P>
</BODY>

//this is correct
<body>
<p>This is a paragraph</p>
</body>
  • Must be properly nested.
  • Must always be closed.
  • Must be in lowercase.
  • XHTML documents must have one root element.

XHTML Attributes

//XHTML attribute names must be in lower case
//this is wrong
<table WIDTH="100%">

//this is correct
<table width="100%">
//attribute values must be quoted
//this is wrong
<table width=100%>

//this is correct
<table width="100%">
//attribute minimization is forbidden
//wrong:
<input type="checkbox" name="vehicle" value="car" checked />

//correct:
<input type="checkbox" name="vehicle" value="car" checked="checked" />
  • Attribute names must be in lower case.
  • Attribute values must be quoted.
  • Attribute minimization is forbidden.

XHTML: Differences from HTML — Structure map

Clickable & Draggable!

XHTML: Differences from HTML — Related pages: