On this page9 sections
Lecture 029: HTML vs XHTML
| Category | 5: Code, Characters & Standards |
| Lecture | 029 of 048 |
| Estimated video length | 6 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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 rule | Example |
|---|---|
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 quoted | width="300" |
| Attributes can't be shortened | checked="checked", not just checked |
Example: valid XHTML-style markup still works in HTML5:
<p>Line one<br />Line two</p>
<input type="checkbox" checked="checked" /> I agree3. HTML5 Today
In HTML5 these are all fine:
<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
- What does XHTML stand for?
- What happens to an XHTML page with one error, when served as XML?
- Write a line break in XHTML style.
- Is
<input checked>valid XHTML? - Which XHTML habits are still good practice?
Practice Answers
- Extensible HyperText Markup Language.
- It fails to display, instead of being quietly corrected.
<br />- No. XHTML requires
checked="checked". - 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.