Explain it simply
Explain it simply
The browser reads an HTML file from top to bottom and, as it reads, draws a family tree. If a <section> contains an <h2> and a <p>, the section is the parent, the h2 and the p are its children, and the h2 and the p are siblings of each other.
That tree is called the DOM (Document Object Model). What you see on screen is not the text file you wrote but a drawing of this tree, so once you understand how the tree is built, you understand why the page looks the way it does.
Problem → Why → Solution
The problem: one missing </a> turns the rest of the page into a link
In the code below, the link in the first paragraph is never closed with </a>. The browser shows no error and the page looks normal. But hover over the following paragraphs and the cursor turns into a hand; click the phone number and you land on the menu page. With no error message anywhere, this bug is hard to track down.
<p>Read our <a href="/menu">menu</p>
<p>We are open every day.</p>
<p>Call 09 123 456 789 to book.</p>Why does this happen?
The browser's parser follows fixed rules. When it reaches </p> it closes the paragraph, but it remembers that the <a> is still open and reopens the link around the content that follows. So in the tree, the remaining paragraphs end up inside the link: the browser did "repair" the markup, just not into the tree you meant.
Core concept: from file to pixels
1. Bytes become characters
The file is a stream of bytes. The browser decodes them into characters using an encoding, usually UTF-8. A wrong encoding is the stage where text such as Myanmar script turns into unreadable symbols.
2. Characters are split into tokens
The tokenizer cuts the characters into start tags (<p>), end tags (</p>), text and comments.
3. Tokens build the tree
Each start tag creates a new node and adds it as a child of the element that is currently open. An end tag closes that element, so the next nodes go back to its parent. Comments also become comment nodes in the tree, but they are never drawn.
4. The tree is styled, laid out and painted
CSS is applied to the tree, the size and position of every box is calculated (layout), and then pixels are drawn (paint). The browser can start these stages before the whole file has finished downloading.
The HTML standard defines the rules of stage 3 in detail, including exactly how to handle broken markup. That is why modern browsers build the same tree from the same broken HTML. The catch is that this tree is not necessarily the one you meant:
| You write | The browser builds |
|---|---|
| <p>One<p>Two | <p>One</p><p>Two</p>: a new p closes the open one |
| <p>Bring:<ul><li>Pen</li></ul></p> | <p>Bring:</p><ul>...</ul><p></p>: a list cannot live in a p |
| <b><i>text</b></i> | <b><i>text</i></b>: the out-of-order end tag is ignored |
| <title>Hi</title><h1>Hi</h1> | html, head and body are added automatically |
Why is HTML so forgiving? In the early web, each browser guessed at broken pages in its own way, and authors ended up with pages that only worked in one browser. The HTML standard ended that by writing down one exact algorithm, error recovery included, for every browser to follow. The forgiveness is a safety net so readers still see a page with a typo in it; it is not permission to write sloppy markup, because the net catches the page in a shape you did not choose.
- Bytes decoded into characters
- Characters split into tokens
- Tokens build the DOM tree
- Tree is laid out and painted
The screen shows the DOM tree, not your file, so when hunting a bug, look at the tree first.
General pattern: nesting
<parent>
<child>first child</child>
<child>second child</child>
</parent>Close elements in the reverse order you opened them: last opened, first closed. Children sit completely between the parent's start and end tags. The indentation is for human readers; the browser ignores it.
Example: reading a section as a tree
This markup is small, but it shows every part of tree building: a comment, one parent and two sibling children. Open it in a browser, press F12 and expand the section in the Elements panel.
DOM tree built from the example
-------------------------------
document
|
+-- <!-- comment --> (kept, never drawn)
|
+-- html
+-- head (added by the browser)
+-- body (added by the browser)
+-- section
+-- h2 "About Me"
+-- p "I am learning HTML step by step."<!-- Browser reads this as a tree -->
<section>
<h2>About Me</h2>
<p>I am learning HTML step by step.</p>
</section>Code breakdown
- Line 1
<!-- Browser reads this as a tree -->The tokenizer recognises a comment token; it becomes a comment node in the tree but is never painted.
- Line 2
<section>Creates a new element node and marks it as open. Until </section> arrives, every following node goes inside this section.
- Line 3
<h2>About Me</h2>h2 becomes the section's first child, and "About Me" is the h2's text child. </h2> closes the h2, so the next element goes back to the section.
- Line 4
<p>I am learning HTML step by step.</p>p is the section's second child and the h2's sibling. Had </h2> been missing, this p would have ended up inside the heading and the whole paragraph would be shown in heading-sized text.
- Line 5
</section>Closes the section; anything after it becomes the section's sibling.
A bold heading "About Me" appears with the sentence "I am learning HTML step by step." below it. The comment is not visible on the page. In the DevTools Elements panel, the section shows h2 and p as its two children.Watch the repairs in DevTools
Save the code below as repair.html and open it. View Source (Ctrl+U) shows it exactly as written, but F12, Elements shows <p>Bring:</p>, then the ul, and finally an empty <p></p>. That empty paragraph is a common source of unexplained extra spacing once CSS margins are applied.
<p>Bring these to class:
<ul>
<li>A notebook</li>
<li>A pen</li>
</ul>
</p>In real projects
Where this bites in real projects
Real sites build HTML from pieces that templates stitch together. One unclosed <div> in a header template can break the layout of every page, and the symptom shows up in the footer even though the bug lives in the template. Text pasted into a CMS from a Word document often carries unclosed tags too.
CSS selectors and JavaScript work on the DOM tree, not on your file. A selector like p > ul never matches even if you wrote the ul inside the p, because the browser moved it out. That is why teams run a validator such as the W3C Nu HTML Checker (validator.w3.org/nu) to catch these problems before users see them.
Common mistakes
Common mistakes
1. "It looks fine in my browser, so it is correct"
It may look fine only because the parser repaired it. The repaired tree can break CSS selectors and scripts, and tools that do not use the standard parser, such as some email clients and HTML-to-PDF converters, may repair it differently. Run a validator.
2. Closing tags in the wrong order
Overlapping tags such as <strong>8:00 <em>every</strong> morning</em> cannot exist in a tree, so the browser splits the em in two. Close in reverse opening order: <strong>8:00</strong> <em>every morning</em>.
3. Forgetting to close a heading
Without </h2> after <h2>Opening hours, every following paragraph ends up inside the h2, shown in heading-sized text and announced by screen readers as one enormous heading. Turn on your editor's auto-close feature.
Best practices
Best practices
- Type the end tag right after the start tag and then fill in the content, or let your editor auto-close tags.
- Indent child elements one level deeper than their parent so an unclosed tag stands out to the eye.
- Run a validator once per finished page and compare the Elements panel tree with your source.
Mini challenge
Mini challenge: repair the tree
The starter code has three markup errors. First predict how the browser will repair each one and check in DevTools, then fix the source so the DOM tree matches it.
- Close the heading properly
- Close strong and em in the right order, with no overlap
- Close the link right after the word "menu"
- Add a paragraph with a phone number and check that it is not inside the link
<!-- TODO: fix the three markup errors so the DOM tree matches the source -->
<section>
<h2>Opening hours
<p>We open at <strong>8:00 <em>every</strong> morning</em>.</p>
<p>See the <a href="#menu">menu</p>
<!-- TODO: add a paragraph with a phone number -->
</section>Expected: "Opening hours" appears at heading size and three paragraphs appear at normal size. Only the word "menu" is a link, and the tree in the Elements panel matches the source exactly.
Show hint
In the Elements panel, check whether the p elements sit inside the h2 and whether more than one a appears. Those are the places the browser repaired.
Show solution
<section>
<h2>Opening hours</h2>
<p>We open at <strong>8:00</strong> <em>every morning</em>.</p>
<p>See the <a href="#menu">menu</a>.</p>
<p>Call 09 123 456 789 to book a table.</p>
</section>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- The browser goes bytes, characters, tokens, DOM tree, layout, paint.
- The page on screen is a drawing of the DOM tree, and CSS and JavaScript work on that tree too.
- Broken markup is repaired by standard rules, but not necessarily into the tree you meant.
- Close tags in reverse opening order and check with the Elements panel and a validator.
HTML Living Standard — HTML Syntax — WHATWG