Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Back to XHTML
XHTML Syntax Rules Explained

XHTML Syntax Rules Explained

XHTMLBy Muhammad Fareed
xhtmlsyntaxrules

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

#RuleWrongRight
1Lowercase element and attribute names<P CLASS="x"><p class="x">
2Close every element<p>One<p>Two<p>One</p><p>Two</p>
3Close empty elements with a slash<br>, <img src="a.png"><br />, <img src="a.png" alt="" />
4Quote all attribute values<div id=main><div id="main">
5No attribute minimization<input checked><input checked="checked" />
6Nest elements properly<b><i>text</b></i><b><i>text</i></b>
7Escape special charactersTom & JerryTom &amp; Jerry
8One 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: &amp;, &lt;, &gt;. This includes URLs with query strings: ?a=1&amp;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 id instead of the deprecated name for identifying elements (except form controls).
  • Use lang and xml:lang together for language.
  • Every img needs an alt attribute.

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 a body cannot hold bare text.

Strict vs Transitional vs Frameset

TypeMeaning
StrictClean structure; presentational tags such as <font> and <center> are not allowed (use CSS)
TransitionalAllows some older presentational elements to ease migration
FramesetFor 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.xhtml to 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.