XHTML

Also known as: Extensible HyperText Markup Language

On this page 7

XHTML, the Extensible HyperText Markup Language, is a reformulation of HTML as an application of XML, which means every document must be well-formed XML before a browser or parser accepts it. It keeps the familiar vocabulary of HTML elements such as paragraphs, links, tables and forms, but replaces the forgiving parsing rules of classic web markup with the strict syntax of XML.

The W3C published XHTML 1.0 in 2000 as a bridge between the HTML 4.01 vocabulary and the wider XML ecosystem. Today the idea survives as the XML serialization of HTML5, sometimes called XHTML5, while most web pages use the ordinary HTML syntax. Understanding XHTML remains useful for EPUB publishing, XML toolchains and any system that must process web content with a standard XML parser.

How XHTML differs from classic HTML parsing

XHTML differs from classic HTML mainly in how strictly the document is parsed. An HTML parser follows error-recovery rules that silently repair unclosed tags, unquoted attributes and misnested elements, whereas an XML parser applies draconian error handling and stops at the first well-formedness error.

The practical consequence is that a single missing end tag can prevent an XHTML page from rendering at all when it is served as XML. Browsers display a parsing error message instead of the content, which makes XHTML predictable for machines but unforgiving for authors who edit pages by hand.

The following table summarizes the syntax rules that XHTML inherits from XML and how they contrast with the tolerant HTML syntax.

RuleXHTML requirementHTML syntax behavior
Element closingEvery element must be closedSome end tags are optional
Void elementsSelf-closing form such as <br />Plain <br> is sufficient
Name caseElement and attribute names in lowercaseNames are case-insensitive
Attribute valuesAlways quotedQuotes optional for simple values
Boolean attributesWritten as checked="checked"Bare checked is allowed
NestingStrictly proper nestingMisnesting repaired by the parser
NamespaceRoot declares the XHTML namespaceNo namespace declaration needed
Core syntax rules of XHTML compared with HTML syntax

The XHTML namespace, http://www.w3.org/1999/xhtml, is declared on the root html element through the xmlns attribute. This declaration lets XML software recognize the elements as XHTML and allows other vocabularies such as SVG and MathML to be mixed into the same document.

Versions and document types of XHTML

XHTML evolved through several specifications, some of which were widely deployed and some of which never reached practical use. Each version defined its own document type declaration, the Doctype line that tells validators which rules apply.

VersionYearPurposeCurrent status
XHTML 1.0 Strict2000HTML 4.01 vocabulary without presentational elementsSuperseded
XHTML 1.0 Transitional2000Allowed legacy elements such as fontSuperseded
XHTML 1.12001Modularized vocabulary, XML media type expectedSuperseded
XHTML Basic2000-2008Subset for mobile devicesObsolete
XHTML 2.0Drafts to 2009Incompatible redesign of the languageAbandoned
HTML5 XML syntax2014 onwardXML serialization of the living HTML standardActive
Main XHTML versions and their status

The abandonment of XHTML 2.0 in 2009 marked a turning point. Browser vendors, working through the WHATWG, favored an evolutionary HTML5 that kept backward compatibility with existing pages, and the W3C eventually aligned with that approach. XHTML did not disappear, however; it became one of two serializations of the same HTML document model.

Why the media type decides how XHTML is parsed

Whether a browser treats a page as XHTML depends on the MIME type sent by the server, not on the Doctype or the syntax inside the file. A document delivered as text/html goes through the HTML parser even if it is perfectly well-formed XHTML, so its XML properties are ignored.

Only the media type application/xhtml+xml triggers the XML parser in browsers. During the 2000s many sites wrote XHTML syntax but served it as text/html, because older versions of Internet Explorer could not render the XML media type. Such pages gained cleaner markup habits but none of the technical benefits of XML processing.

Served media typeParser usedError handlingTypical use
text/htmlHTML parserErrors silently repairedOrdinary websites
application/xhtml+xmlXML parserFatal error on malformed markupXML-based publishing
application/xmlGeneric XML parserFatal error, weaker semanticsRare, tooling cases
Effect of the served media type on an XHTML file

The XML parsing mode also changes some scripting and DOM behavior. For example, document.write() is not supported, element names in the DOM keep their lowercase form, and content inside script elements must be escaped or wrapped in a CDATA section when it contains characters such as < or &.

Where XHTML is still used today

XHTML remains relevant wherever machine processing matters more than tolerance. Its predictable structure lets standard XML tools such as XSLT, XPath and XQuery transform and query documents without a custom HTML parser.

  • EPUB 3 e-books require content documents in the XHTML syntax of HTML5, so every chapter of a reflowable e-book is an XHTML file.
  • Content management and documentation pipelines often store articles as XHTML so they can be validated and transformed with XSLT.
  • Embedded and specialized user agents, including some e-readers and print formatters, rely on XML parsers for simplicity.
  • Mixed-namespace documents combine XHTML with SVG, MathML or custom XML vocabularies in one file.

For general websites, the HTML syntax of HTML5 is the practical default. Developers who want strict, readable markup usually achieve it through HTML validation and linting tools rather than by switching to the XML media type.

Common mistakes when authoring XHTML

Most XHTML problems stem from confusing syntax with parsing mode or from habits carried over from tolerant HTML. The table below lists frequent symptoms, their causes and the correct fix.

SymptomCauseFix
Yellow XML error pageUnclosed or misnested elementValidate and close every element
Entity error on `&nbsp;`Named entity unknown to the XML parserUse a numeric reference or the character itself
Script fails to runUnescaped < or & in inline scriptUse a CDATA section or an external file
Styles ignoredUppercase element names in markupWrite all names in lowercase
No XML behavior at allPage served as text/htmlSend application/xhtml+xml
Frequent XHTML errors and how to correct them

Another misconception is that XHTML is inherently faster or more semantic than HTML. The vocabulary and semantics are the same; the difference lies only in serialization and error handling, so semantic quality depends on the author choosing appropriate elements in either syntax.

In simple terms

XHTML is the same language used to build web pages, but written under much stricter grammar rules. Ordinary HTML is like a friendly editor who quietly fixes your typos. XHTML is like a strict examiner who refuses the whole page if a single tag is left open.

That strictness makes life harder for people typing pages by hand, but easier for computer programs that need to read, check and convert documents. This is why XHTML lives on mainly in e-books and publishing systems, while most websites use regular HTML.

A practical example

A small academic publisher converts a 300-page textbook into an EPUB 3 e-book. Each of the 24 chapters becomes a separate XHTML file with the XHTML namespace on the root element, lowercase tags and self-closing image elements, and mathematical formulas are embedded directly as MathML.

Before release, the production team runs an EPUB validator over the package. It reports two fatal errors: a paragraph closed in the wrong order around an emphasized phrase and an &nbsp; entity that the XML parser does not recognize. After the editor closes the elements correctly and replaces the entity with a numeric character reference, the book passes validation and displays consistently on e-readers. The result is a single well-formed source that the publisher also transforms with XSLT into print-ready output.