
Consistent HTML makes a page easier to read, maintain, and test. This guide covers everyday HTML5 conventions, compares them with XHTML, and explains when XML namespaces matter.
1. HTML5 rules and conventions
| Topic | What to do |
|---|---|
| Document structure | Use an HTML doctype. Organize the page with <html>, <head>, and <body>. |
| Tag names | HTML tag names are case-insensitive. Lowercase is the usual convention. |
| Special characters | Use character references where a character would be treated as markup. For example, < renders as a less-than sign. |
| Indentation | Indent nested elements so that the structure is easy to review. If you minify the deployed file, preserve meaningful spaces and preformatted text. |
| Whitespace | Normal HTML rendering collapses most consecutive spaces, tabs, and newlines. Use CSS for spacing, <br> for a meaningful line break, and where a line break is undesirable. |
| Attribute values | Use consistent quotes around attribute values, for example <a href="page.html">Link</a>. |
| Validation and testing | Use an HTML checker to catch invalid markup, then test in different browsers and on different devices. |
| Search and accessibility | Use meaningful headings, descriptive links, and suitable metadata. |
2. XHTML rules
XHTML expresses HTML using XML syntax. XML parsers expect well-formed markup, which is why some syntax accepted in HTML produces an error in a document served as XHTML.
| Rule | Example |
|---|---|
| Use the doctype relevant to the document type | An XHTML 1.0 Strict document uses the declaration shown below. |
| Use lowercase names | Write <p>, not <P>. |
| Nest and close tags | Write <p>Text with <em>emphasis</em>.</p>. |
| Quote attribute values | Either matching single quotes or matching double quotes are valid. |
| Use empty-element syntax | Write <img src="image.jpg" alt="Example image" /> or <br />. |
| Write Boolean attribute values explicitly | <option selected="selected">Item</option> instead of a bare selected. |
| Escape special characters | Use < or > where needed. |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
The same document in HTML and XHTML
The HTML example uses the short doctype and a void line-break element. The XHTML version declares the XHTML namespace and closes the empty element using XML syntax.
<!DOCTYPE html>
<html>
<head>
<title>HTML example</title>
</head>
<body>
<h1>Article title</h1>
<p>This is an example paragraph.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<br>
</body>
</html>
<!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">
<head>
<title>XHTML example</title>
</head>
<body>
<h1>Article title</h1>
<p>This is an example paragraph.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<br />
</body>
</html>
3. Is XHTML still used?
Most new websites use HTML served as text/html. XHTML is still relevant in legacy projects and XML-based workflows. The media type matters: browsers apply HTML parsing rules to text/html even when the file uses some XHTML-style syntax. When serving actual XHTML, a well-formedness error may prevent the document from rendering.
4. Why namespaces matter
An XML namespace identifies the vocabulary an element belongs to, which avoids conflicts when a document combines multiple vocabularies. XHTML uses xmlns="http://www.w3.org/1999/xhtml". SVG uses its own namespace. Here is an SVG example:
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
Continue with HTML validation and verification tools.