Comments
Browsers are remarkably forgiving of invalid HTML — they'll often render something reasonable-looking even when the underlying markup is technically broken. That forgiveness is also exactly why bugs in HTML structure can go unnoticed for a long time. This chapter covers comments, whitespace conventions, and how to actually verify a document is valid rather than just "looks fine in one browser."
HTML Comments
Useful for:
- Marking section boundaries in a long file —
<!-- End of header -->after a long closing block. - Leaving notes for future maintainers (including future you) about why something is structured a particular way.
- Temporarily disabling a block during development, without deleting it outright.
Whitespace — Why It (Mostly) Doesn't Matter, and When It Does
Browsers collapse multiple consecutive whitespace characters (spaces, tabs, newlines) into a single space when rendering text content — this is why indentation and line breaks in the source code are purely for human readability, not something the browser preserves visually by default.
The one common exception: the <pre> element preserves whitespace and line breaks exactly as written — used for displaying code samples or any text where exact formatting genuinely matters.
Consistent Indentation — A Convention, Not a Rule
Chapter 1 already introduced the idea that browsers don't require indentation at all, but consistent indentation makes nested structure genuinely readable to a human. Most projects pick either 2 or 4 spaces and apply it consistently — the specific number matters far less than actually being consistent throughout a project.
Why "It Works in My Browser" Isn't the Same as Valid
Browsers' forgiving error-recovery behaviour means genuinely broken HTML — an unclosed tag, a missing required attribute, improperly nested elements — often still renders something acceptable-looking, hiding the underlying problem until it causes a real issue (often in a different browser, or after a future edit makes the existing error finally matter).
Using the W3C Validator
The W3C Markup Validation Service checks a page (by URL or pasted code) against the official HTML specification and reports every actual error and warning, with line numbers.
Common Validation Mistakes
Chapter 10 Quick Reference
- <!-- comment --> — never visible to visitors, but visible in page source; never put sensitive info here
- Whitespace collapses to a single space when rendered, except inside <pre>
- Consistent indentation is purely for human readability — pick 2 or 4 spaces, apply consistently
- Browsers' error tolerance hides real bugs — "renders fine" isn't the same as "valid"
- W3C Markup Validation Service — checks real specification compliance, with line numbers
- Common errors: unclosed tags, improper nesting, missing required attributes, duplicate IDs
- Validate periodically during development, not just once at the end
- Next chapter: special characters & entities