HTML Validation and Debugging
Why browsers never reject bad HTML, how the parser repairs it silently, and how the Nu validator and DevTools show what you wrote versus what you got.
Language, Direction and InternationalizationForms, Labels and Submission
HTML Validation and Debugging
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- HTML has no syntax errors the browser will stop on. The parser repairs bad markup by fixed rules, so a broken page still renders, just not the way you wrote it.
- The DOM is what the parser built, not your source. In a test `<p>one<div>two</div>three</p>` became a paragraph, a div, loose text and an empty paragraph.
- An unclosed quote can swallow the rest of the document. `<a href="x>link</a><p>after</p>` produced no elements at all in one test.
- The Nu HTML Checker reports what the parser silently fixed or tolerated: bad nesting, duplicate ids, missing `alt`, obsolete elements, skipped headings.
- Debug in DevTools' Elements panel, which shows the live DOM, and compare with View Source, which shows what you sent.
A language with no syntax errors
Most languages stop at the first typo. HTML never does. Forget a closing tag, nest things the wrong way round, mistype an attribute, and the browser still shows something. This was a design decision: early browsers had to cope with sloppy hand-written pages, so the HTML spec now defines exactly how to recover from every mistake, and all engines follow the same algorithm.
The upside is forgiveness. The downside is that bugs hide: the page looks nearly right, and a parser repair three elements up causes the layout problem you are chasing.
What the parser did to my markup
I fed fragments through innerHTML in Chromium 159 and printed what was built:
| Source | Built DOM |
|---|---|
<p>one<div>two</div>three</p> |
<p>one</p><div>two</div>three<p></p> |
a</p>b |
a<p></p>b |
<b>x<i>y</b>z</i> |
<b>x<i>y</i></b><i>z</i> |
<a href=1>one<a href=2>two</a></a> |
two sibling links |
<table>stray<tr><td>cell</td></tr></table> |
stray<table><tbody>…</tbody></table> |
<p id=a id=b class=c class=d>x</p> |
<p id="a" class="c">x</p> |
<div/>one</div> |
<div>one</div> |
<a href="x>link</a><p>after</p> |
nothing at all |
Read them as rules. A block start tag closes an open <p>, so the later </p> makes an empty one. Stray text in a table is moved in front of it. The first duplicate attribute wins. A self-closing <div/> does not close the div. And an unclosed attribute quote runs to the end of the input, so the whole tag and everything after it vanished. Nesting <div> inside <span> was accepted by the parser as written, and that is where the validator comes in: the parser only repairs what it must.
Duplicate ids don’t error either: getElementById returned the first match, and querySelectorAll('#x') found both.
The validator says what the parser tolerated
The Nu HTML Checker (vnu.jar 26.11.20, the engine behind validator.w3.org/nu) on a short page flagged: div not allowed in span, a </p> with no open p, a button inside an a, an img without alt, a duplicate id (with a pointer to the first), a div as a child of ul, the obsolete center, a missing lang, and a skipped heading level (h1 to h3). Each message has a line and column. It did not flag <div role="button"> or a positive tabindex, so validation is a floor, not a verdict (testing accessibility). Form lessons help here too: labels and submission shows markup that validates but leaves inputs unnamed.
Run it in CI, or as an editor linter. A check on every commit catches nesting mistakes before they reach a layout bug.
Debugging in practice
- Elements panel, not View Source. The panel is the DOM after parsing and script. If it differs from your source, a repair happened.
- Find the first wrong node. Parser repairs cascade, so check the earliest oddity, often an empty
<p>or loose text. - Validate. The line numbers point at the cause.
- Check the tree. For accessibility problems, compare with the accessibility tree.
- Isolate. Cut the page down until the problem is small. CSS issues are in debugging CSS with DevTools.
I only ran the parser table in Chromium. The HTML spec makes the recovery rules uniform, but I did not confirm each case in the other engines.
What the video covers
Saalim Al Marhubi’s “Debugging HTML” from the series “HTML for the Mandem” (June 2024, about 13 minutes). The chapters are: debugging isn’t scary, HTML and debugging, permissive code, a debugging example, browser dev tools, how browsers parse HTML errors, fixing errors, HTML validation, and an outro. That matches the lesson well. It is a smaller channel and I could not read a transcript, so I do not describe the example. The parser table and validator output above are my own runs. The Google Search Central talk on parsing is the runner-up.
Where this fits
This follows language and direction and prepares performance and writing maintainable HTML.
Check yourself
Why does a page with a misspelt or misnested tag still render?
HTML parsing is defined to never fail. The spec gives every malformed case a recovery rule, so browsers agree on the result. In a test, `<b>x<i>y</b>z</i>` became `<b>x<i>y</i></b><i>z</i>`: the italic was reopened after the bold closed.
Your layout has stray empty paragraphs and a block seems to have escaped its parent. How do you find out why?
Open DevTools' Elements panel and read the live DOM. A `<div>` inside a `<p>` closes the paragraph, leaving loose text and an empty `<p>` after, as in the test. Then run the validator to get the line number.
What is the difference between View Source and the Elements panel?
View Source shows the bytes the server sent. The Elements panel shows the DOM after parsing and script changes, including repairs the parser made. Debug against the DOM.
Is a page that validates also accessible?
No. The validator checks syntax and content-model rules. In a test it did not flag a `div role="button"` with no keyboard support. It does catch missing `alt` and duplicate ids, which matter for access.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.