Skip to main content

HTML Versus XHTML

XHTML is a stricter, more XML-based version of HTML.


What is XHTML?

XHTML stands for EXtensible HyperText Markup Language. It is a stricter, more XML-based version of HTML; in fact, it is HTML defined as an XML application. It is supported by all major browsers.


Why XHTML?

XML is a markup language where every document must be marked up correctly, or "well-formed". XHTML was created to make HTML more extensible and able to work with other data formats like XML. Browsers ignore errors in HTML and try to show the page anyway, but XHTML uses much stricter error handling.


The Most Important Differences from HTML

The main differences are: the <!DOCTYPE> is mandatory; the xmlns attribute on <html> is mandatory; <html>, <head>, <title>, and <body> are mandatory; elements must always be properly nested; elements must always be closed; element and attribute names must be lowercase; attribute values must be quoted; and attribute minimization is forbidden.


XHTML - <!DOCTYPE ....> Is Mandatory

An XHTML document must have an XHTML <!DOCTYPE> declaration. The <html>, <head>, <title>, and <body> elements must be present, and the xmlns attribute on <html> must give the XML namespace.

Minimal XHTML document

An XHTML document with the minimum required tags.

index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title of document</title>
</head>
<body>

some content here...

</body>
</html>

XHTML Elements Must be Properly Nested

In XHTML, elements must always be properly nested. So <b><i>Some text</i></b> is correct, while closing them in the wrong order is not.


XHTML Elements Must Always be Closed

In XHTML, every element must be closed. Each paragraph needs a closing </p> tag, for example; leaving paragraphs open is not allowed.


XHTML Empty Elements Must Always be Closed

In XHTML, empty elements must also be closed, by ending the tag with a slash, like <br />, <hr />, and <img ... />.

Closed empty elements

Empty elements written the XHTML way, closed with a slash.

index.html
A break: <br />
A horizontal rule: <hr />
An image: <img src="happy.gif" alt="Happy face" />

XHTML Elements Must be in Lowercase

In XHTML, element names must always be lowercase, so <body> and <p>, not <BODY> and <P>.


XHTML Attribute Names Must be in Lowercase

In XHTML, attribute names must always be lowercase, so href, not HREF.


XHTML Attribute Values Must be Quoted

In XHTML, attribute values must always be quoted, so write the value of href in quotes rather than leaving it bare.


XHTML Attribute Minimization is Forbidden

In XHTML, attribute minimization is forbidden, so you must write the full pair like checked="checked" and disabled="disabled" rather than just checked or disabled.

Full attribute pairs

Boolean attributes written in full, as XHTML requires.

index.html
<input type="checkbox" name="vehicle" value="car" checked="checked" />
<input type="text" name="lastname" disabled="disabled" />

Validate HTML With The PromptCookbook Validator

PromptCookbook includes a box where you can enter a web address to validate your HTML with the PromptCookbook validator.