Skip to content
Tutorials Center
My learning

Topics

Lesson 110 of 122 ยท Accessibility and Best Practices

HTML vs XHTML

Learn how HTML and XHTML differ, the strict rules of the XML syntax, why the MIME type decides how a page is parsed, and old versus modern doctypes.

If you read older tutorials or maintain an older site, you will run into XHTML: pages full of <br />, xmlns attributes and long doctypes that mention DTDs. You may also hear that XHTML is "stricter" or "more correct". This lesson explains what XHTML actually is, how it differs from ordinary HTML, and why nearly every site today is served as plain HTML.

A bit of history

Around 2000, the W3C believed the web's future was XML, a strict, general-purpose markup language. It reformulated HTML 4 as an XML language and called it XHTML 1.0. The tags were the same; the syntax rules were XML's. XHTML 1.1 and a planned, incompatible XHTML 2.0 followed, but XHTML 2.0 was abandoned in 2009 as browser makers backed what became HTML5. The history lesson tells the full story.

Today the HTML Living Standard defines one language with two syntaxes:

  • the HTML syntax, used by almost every web page;
  • the XML syntax, often still called XHTML.

Both describe the same elements and the same DOM. What differs is how the text of the file is written and parsed.

The MIME type decides, not the markup

Here is the part that surprises most people: writing your page in XHTML style does not make it XHTML. What decides is the MIME type the server sends in the Content-Type header.

HTTP
Content-Type: text/html; charset=utf-8

With text/html, the browser uses the forgiving HTML parser, whatever the file looks like. Your <br /> and xmlns are accepted, and then mostly ignored.

HTTP
Content-Type: application/xhtml+xml

With application/xhtml+xml (or another XML type), the browser uses its XML parser. Now the XML rules really apply. Most servers pick this type for files ending in .xhtml.

Most "XHTML" sites were really HTML. During the 2000s, many sites used XHTML doctypes but were served as text/html, partly because Internet Explorer before version 9 could not display application/xhtml+xml. Browsers parsed them as ordinary HTML all along.

The rules of the XML syntax

When a page is parsed as XML, it must be well-formed. These rules apply:

Rule HTML syntax XML syntax
Closing tags Some may be omitted (</p>, </li>) Every element must be closed
Void elements <br> <br/> or <br />
Case Tag names are case-insensitive Case-sensitive; HTML elements are lowercase
Attribute values Quotes sometimes optional Always quoted
Boolean attributes <input disabled> <input disabled="disabled"/>
Namespace Not needed xmlns="http://www.w3.org/1999/xhtml" on <html>
Nesting Errors are repaired Elements must close in the reverse order they opened
Special characters A lone & is often tolerated & and < in text must always be escaped

A complete document in the XML syntax looks like this:

XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <title>Shipping rates</title>
  </head>
  <body>
    <p>Free shipping on orders over &#8364;50.<br/>Rates &amp; times below.</p>
    <input type="checkbox" checked="checked"/>
  </body>
</html>

The XML declaration on the first line is optional when the file is UTF-8. Note &#8364; for the euro sign: XML only predefines five named entities (&lt;, &gt;, &amp;, &quot;, &apos;), so numeric references or plain UTF-8 characters are the safe choice.

Draconian error handling

The biggest practical difference is what happens with a mistake. The HTML parser repairs errors, as you saw in Validating Your HTML. The XML parser stops. A single unclosed tag or unescaped & means the browser shows an XML error message instead of your page. Developers nicknamed this "draconian" error handling.

You can see both parsers at work right in the browser. DOMParser can parse text as HTML or as XML. Edit the markup and press the buttons:

HTML parser vs XML parser

The HTML parser accepts everything and repairs it. The XML parser rejects it. Now fix the markup to satisfy XML: write &amp;, close the paragraph, use <br/>, write checked="checked" and close the input with />, and correct the nesting. Then both parsers agree.

Doctypes, old and new

The doctype is the first line of an HTML document. In the HTML 4 and XHTML 1 era, it named a DTD (a machine-readable grammar) and came in several flavors:

HTML
<!-- HTML 4.01 Strict -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">

<!-- HTML 4.01 Transitional (allowed presentational markup) -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">

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

Browsers never actually downloaded or used those DTDs. They only looked at the doctype to pick a rendering mode:

  • No-quirks (standards) mode: modern, standard behavior.
  • Limited-quirks mode: standard behavior with one old quirk around images in table cells.
  • Quirks mode: imitates the bugs of late-1990s browsers, such as a different box model for some elements, so ancient pages keep working.

A missing doctype, or certain old ones, triggers quirks mode. The modern doctype is designed to be the shortest string that reliably selects standards mode in every browser:

HTML
<!doctype html>

It has no version number, because HTML no longer has versions. You can check which mode a document is in with document.compatMode: "CSS1Compat" means standards or limited-quirks mode, and "BackCompat" means quirks mode.

Which doctype gives which mode?

Always start with <!doctype html>. It costs nothing and guarantees standards mode. Most of the "my CSS works differently in this one page" mysteries in old projects come down to a missing or unusual doctype. The style guide puts it on the first line of every page.

Pages in the XML syntax are never rendered in quirks mode, and they do not need a doctype at all. In the other direction, if an XML-based tool generates text/html pages and cannot write the short doctype, the standard allows a longer legacy form, <!DOCTYPE html SYSTEM "about:legacy-compat">, which also selects standards mode.

Should you use the XML syntax?

For ordinary websites, no. Serving text/html is simpler, more robust and what every tool expects. One typo should not blank your entire page.

The XML syntax still has a place:

  • EPUB e-books use XHTML documents inside the book package.
  • Pipelines built on XML tools (XSLT, XQuery, some content systems) can produce and process XHTML directly.
  • Embedding inside other XML, such as XHTML inside an Atom feed or an SVG <foreignObject>.

It also helps to know the XML habits that are harmless in HTML. Writing <br /> or <img ... /> in a text/html page is allowed: the slash on a void element is simply ignored. Inline <svg> and <math> elements even accept real self-closing tags such as <circle ... />, because they follow XML-like rules inside HTML. But as the style guide showed, a slash on a normal element like <div /> does not close it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What decides whether a browser parses a page as HTML or as XML?

    text/html always uses the HTML parser. application/xhtml+xml uses the XML parser, regardless of what the markup looks like.
  2. 2. What happens when an XHTML page served as application/xhtml+xml has an unclosed tag?

    XML parsing is draconian. A page that is not well-formed is not displayed.
  3. 3. How do you write a checked checkbox in the XML syntax?

    In XML every attribute needs a quoted value, empty elements must be closed, and names are case-sensitive and lowercase for HTML elements.
  4. 4. What does document.compatMode return for a page in quirks mode?

    "BackCompat" means quirks mode. "CSS1Compat" covers both standards mode and limited-quirks mode.

Summary

  • XHTML is HTML written in XML syntax; the HTML standard defines both syntaxes for one language.
  • The MIME type decides the parser: text/html is forgiving, application/xhtml+xml is strict.
  • The XML syntax requires closed elements, quoted values, lowercase names, the XHTML namespace and escaped & and <.
  • XML parsing stops at the first error; HTML parsing repairs and continues.
  • Old doctypes named DTDs but really only chose a rendering mode; <!doctype html> reliably selects standards mode.
  • For normal websites, serve HTML as text/html; the XML syntax is mainly for EPUB and XML toolchains.

Next up: Deprecated and Obsolete Features.