On this page9 sections
  1. Learning Objectives
  2. 1. What XHTML Is
  3. 2. The Rules XHTML Requires
  4. 3. HTML5 Today
  5. 4. Habits Worth Keeping
  6. Common Beginner Mistakes
  7. Practice Questions
  8. Practice Answers
  9. Key Takeaways

Lecture 029: HTML vs XHTML

Category5: Code, Characters & Standards
Lecture029 of 048
Estimated video length6 min
TargetHTML5 · any modern browser
LevelBeginner

In the early 2000s, XHTML tried to make HTML stricter by following the rules of XML. HTML5 won in the end, but XHTML's good habits live on in clean code today. You'll still meet XHTML in old projects and in some tools, so it helps to recognise it.


Learning Objectives

  • Explain what XHTML is and how it differs from HTML
  • Recognise XHTML-style code such as <br />
  • Know which XHTML habits are still good practice

1. What XHTML Is

XHTML = EXtensible HyperText Markup Language: HTML rewritten as XML. XML is strict: one mistake and the whole page fails to display, instead of the browser quietly fixing it.


2. The Rules XHTML Requires

XHTML ruleExample
The doctype and xmlns are required<html xmlns="http://www.w3.org/1999/xhtml">
Elements must always be closed<p>…</p>, never a lone <p>
Empty elements must close themselves<br />, <img … />
Elements must be properly nested<b><i>text</i></b>
Tags and attributes must be lowercase<a href="…">
Attribute values must be quotedwidth="300"
Attributes can't be shortenedchecked="checked", not just checked

Example: valid XHTML-style markup still works in HTML5:

html
<p>Line one<br />Line two</p>
<input type="checkbox" checked="checked" /> I agree

3. HTML5 Today

In HTML5 these are all fine:

html
<br>
<img src="cat.jpg" alt="Cat">
<input type="checkbox" checked>

Browsers accept both styles. The self-closing slash (<br />) is allowed but does nothing in HTML5.


4. Habits Worth Keeping

Even though HTML5 is forgiving, these XHTML rules still make code cleaner (see Lecture 026):

  • Close every element that has a closing tag.
  • Nest elements properly.
  • Write lowercase.
  • Quote every attribute value.

Common Beginner Mistakes

1. Thinking <br /> is required. It's optional in HTML5. Pick one style and be consistent.

2. Thinking XHTML is "more modern". HTML5 replaced it; new projects use HTML5.


Practice Questions

  1. What does XHTML stand for?
  2. What happens to an XHTML page with one error, when served as XML?
  3. Write a line break in XHTML style.
  4. Is <input checked> valid XHTML?
  5. Which XHTML habits are still good practice?

Practice Answers

  1. Extensible HyperText Markup Language.
  2. It fails to display, instead of being quietly corrected.
  3. <br />
  4. No. XHTML requires checked="checked".
  5. Closing elements, proper nesting, lowercase names and quoted attribute values.

Key Takeaways

  • XHTML is HTML written with strict XML rules.
  • HTML5 replaced it, and is more forgiving.
  • <br /> and <br> both work in HTML5.
  • Keep XHTML's good habits: close, nest, lowercase, quote.