XHTML Syntax Rules Explained
Short answer: because XHTML is XML, every document must be well-formed: one root element, all tags lowercase and properly nested, every element closed, all attribute values quoted, and no attribute without a value. Break a rule and an XML parser reports an error.
The core rules
| # | Rule | Wrong | Right |
|---|---|---|---|
| 1 | Lowercase element and attribute names | <P CLASS="x"> | <p class="x"> |
| 2 | Close every element | <p>One<p>Two | <p>One</p><p>Two</p> |
| 3 | Close empty elements with a slash | <br>, <img src="a.png"> | <br />, <img src="a.png" alt="" /> |
| 4 | Quote all attribute values | <div id=main> | <div id="main"> |
| 5 | No attribute minimization | <input checked> | <input checked="checked" /> |
| 6 | Nest elements properly | <b><i>text</b></i> | <b><i>text</i></b> |
| 7 | Escape special characters | Tom & Jerry | Tom & Jerry |
| 8 | One root html element with the XHTML namespace | <html> | <html xmlns="http://www.w3.org/1999/xhtml"> |
Required document structure
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Page title</title>
</head>
<body>
<h1>Heading</h1>
</body>
</html>
The XML declaration is optional but recommended, and the DOCTYPE must be one of the XHTML types (Strict, Transitional or Frameset in XHTML 1.0).
Details that trip people up
Special characters
The characters &, < and > must be written as entities in text and attribute values: &, <, >. This includes URLs with query strings: ?a=1&b=2.
Scripts and styles
Inline JavaScript with < or & breaks XML parsing. Wrap it in a CDATA section, or better, load it from an external file:
<script type="text/javascript">
//<![CDATA[
if (a < b && c > d) { run(); }
//]]>
</script>
Attributes
- Use
idinstead of the deprecatednamefor identifying elements (except form controls). - Use
langandxml:langtogether for language. - Every
imgneeds analtattribute.
Element content rules
- Block elements (
div,p,form) must not appear inside inline elements. - In Strict, a
form's direct children must be block-level, and abodycannot hold bare text.
Strict vs Transitional vs Frameset
| Type | Meaning |
|---|---|
| Strict | Clean structure; presentational tags such as <font> and <center> are not allowed (use CSS) |
| Transitional | Allows some older presentational elements to ease migration |
| Frameset | For pages using frames, which are obsolete |
What happens when you break the rules?
If the page is served as application/xhtml+xml, browsers use an XML parser and show an error message instead of the page. When served as text/html, they fall back to HTML parsing, hide the problem and ignore the extra slashes.
How to check your code
- Use the W3C Markup Validator or a linter.
- Run an XML parser such as
xmllint --noout page.xhtmlto confirm it is well-formed. - Test in more than one browser.
Frequently asked questions
Do I need to close tags like br and img in XHTML?
Yes. Empty elements must be self-closed, written as <br /> and <img ... />, because XML requires every element to be closed.
How can I validate my XHTML?
Use the W3C Markup Validation Service or an XML parser. Both report errors such as unclosed tags, improper nesting and missing quotes around attribute values.
Next steps
See the bigger picture in what XHTML is, follow XHTML best practices and read how to convert HTML to XHTML.
