XHTML
XHTML reformulated HTML 4 as strict XML. Learn its well-formedness rules, doctypes and xmlns, plus why HTML5 has largely superseded it.
XHTML (Extensible HyperText Markup Language) is a 2000-era reformulation of HTML 4.01 as a strict XML application, published by the World Wide Web Consortium (W3C). It has the same vocabulary of tags as HTML, but enforces XML's well-formedness rules: every tag must be closed, lowercase, and properly nested.
For new projects, prefer HTML5. XHTML 1.0 has been largely superseded by HTML5, which keeps the well-formedness discipline as an optional polyglot style while being far more forgiving and feature-rich. This page is best understood as background on a legacy standard you may still encounter in older codebases.
The original goal was to make documents stricter and more predictable, so they could be parsed by any standard XML processor and reused across a range of devices and tools. Because the rules are unforgiving, a single missing closing tag could stop the whole document from rendering when served as real XML.
Well-formedness rules
The defining difference between HTML and XHTML is that XHTML must be well-formed XML. Below are the rules, each shown as wrong vs. right.
Document structure and doctype
Every XHTML document must start with an XHTML <!DOCTYPE> and contain the <html>, <head>, <title>, and <body> elements. The <html> element must also declare the XML namespace with the xmlns attribute.
<!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 the document</title>
</head>
<body>
Content of the page
</body>
</html>The xmlns attribute
xmlns="http://www.w3.org/1999/xhtml" is the XML namespace declaration. It tells an XML parser that the elements in the document belong to the XHTML vocabulary rather than some other XML language. It is required on the root <html> element of every XHTML document. The URL is an identifier, not a link the browser fetches.
XHTML doctype variants
XHTML 1.0 defines three DOCTYPEs, each pointing at a different DTD. The example above uses Transitional, the most common choice.
- Strict — clean, presentation-free markup. Deprecated presentational elements and attributes (such as
fontorbgcolor) are not allowed; styling is left to CSS. - Transitional — permits the deprecated presentational features for compatibility with older content.
- Frameset — like Transitional, but allows
<frameset>documents that split the window into frames.
Close every element
In HTML some closing tags can be omitted, but in XHTML every element must be closed. Void elements that have no content (like br, hr, img) close themselves with a trailing slash.
<!-- Wrong (HTML-style, not closed) -->
A break: <br>
An image: <img src="smile.gif" alt="Always smile">
<!-- Right (self-closed for XHTML) -->
A break: <br />
An image: <img src="smile.gif" alt="Always smile" />Use lowercase tag and attribute names
XML is case-sensitive, so <P> and <p> are different tags. XHTML requires all element and attribute names in lowercase.
<!-- Wrong -->
<BODY>
<P>Some paragraph</P>
</BODY>
<!-- Right -->
<body>
<p>Some paragraph</p>
</body>Quote every attribute value
All attribute values must be wrapped in quotes, even numeric ones.
<!-- Wrong -->
<table width=100%>
<!-- Right -->
<table width="100%">Nest elements properly
Overlapping tags are forbidden; inner elements must close before their outer parent.
<!-- Wrong (overlapping) -->
<strong><em>This text is bold and italic</strong></em>
<!-- Right (properly nested) -->
<strong><em>This text is bold and italic</em></strong>No attribute minimization
In HTML, boolean attributes can stand alone (checked). XHTML forbids this shorthand: every attribute needs a value, and for booleans the value repeats the name.
<!-- Wrong (minimized) -->
<input type="checkbox" name="flower" value="rose" checked />
<!-- Right -->
<input type="checkbox" name="flower" value="rose" checked="checked" />Prefer id over name
In XHTML the name attribute is deprecated on elements like anchors, so the id attribute is recommended for identifying elements instead.
Benefits of XHTML
- All tags must be closed and properly nested, which keeps the markup consistent and easier to read.
- Being well-formed XML, documents could be processed reliably by generic XML tools and transported to specialized environments such as Braille readers and other assistive software.
- It encourages a clean separation of structure and presentation, working alongside CSS for styling.
XHTML vs HTML
A few key distinctions between the two:
- Underlying standard. HTML is an application of SGML; XHTML is an application of XML.
- Lineage. HTML descends from SGML; XHTML is derived from both XML and HTML 4.
- History. HTML originated with Tim Berners-Lee in the early 1990s (Wikipedia); XHTML 1.0 became a W3C Recommendation in 2000 (Wikipedia).
- Parsing. HTML uses a lenient, error-tolerant parser; XHTML served as XML requires a strict XML parser that rejects malformed documents.
XHTML vs HTML5
- Case sensitivity. XHTML is case-sensitive; HTML5 (like classic HTML) is not.
- Doctype. XHTML doctypes are long and reference a DTD; HTML5 uses the short
<!DOCTYPE html>. - MIME type. HTML5 is served as
text/html; strict XHTML parsing requiresapplication/xhtml+xml. - Tolerance. HTML5 recovers gracefully from many markup errors, whereas XHTML rejects them.
- Status. HTML5 is the modern standard for all devices; XHTML 1.0 is now considered legacy.