Nesting Rules and Content Models
Type a paragraph, press enter in your editor, and put a div inside it. The browser does not complain. It quietly closes the paragraph, opens the div as a sibling, and renders something that looks roughly like what you expected, which is precisely why invalid nesting survives into production: the failure is invisible on screen and expensive everywhere else.
That behaviour comes from the tree construction stage of the parser. The specification defines, for each element, exactly what may appear inside it, and when the incoming markup violates that contract the parser applies a recovery rule. Some rules are local and harmless, such as implicitly closing a paragraph when a block element arrives. Others are destructive, such as the algorithm that untangles nested interactive elements by partially closing and reopening nodes, which can leave your DOM in a state that differs from the source you wrote.
The vocabulary that describes these contracts is called content models, and there are two families worth memorising:
- flow content is the general body material: paragraphs, lists, divs, tables, and most of what fills a page
- phrasing content is the text-level material: spans, emphasis, images, links, and anything that belongs inside a single line of text without a block break
An element that only accepts phrasing content will reject a div, and the parser will close the parent rather than nest the block inside it. Knowing which family an element expects tells you, in one step, whether your markup is legal.
The Content Models in One Page
The table below covers the elements most templates get wrong, together with what the parser does when the rule is broken.
| Element | Allowed children | What the parser does on violation | Visible symptom |
|---|---|---|---|
| Paragraph | Phrasing content | Closes the paragraph before the block | An unexpected gap in the text |
| List | List items only | Ignores stray text nodes | Bullet alignment breaks |
| Definition list | dt and dd pairs | Keeps text but it never renders as a term | Semantics lost entirely |
| Table | Caption, colgroup, thead, tbody, tfoot | Moves rows into an implied body | Borders and styles miss rows |
| Table row | Cells and script-supported elements | Drops the cell into an implied row | Columns shift |
| Anchor | Nothing interactive, no other anchor | Untangles the nesting by splitting nodes | Links behave unpredictably |
| Button | Phrasing content, no buttons | Closes the outer button first | Clicks hit the wrong target |
| Figure | One figcaption, plus flow content | Keeps the caption but loses which is which | Caption detaches from the image |
The row that causes the most support tickets is the anchor rule. Two links cannot be nested, so a card built as a single anchor containing a "read more" anchor is silently rewritten: the inner link is hoisted out, and the click target the developer tested is no longer the element receiving the event.
How the Parser Recovers, in Practice
Two recovery rules account for most of the surprises you will see in a rendered document. The first is the implied end tag. When a block element arrives while a paragraph is still open, the parser closes the paragraph, inserts the block as a sibling, and carries on. The source may show a paragraph containing a div, but the DOM contains two siblings, and any selector written against a descendant relationship silently matches nothing.
The second is the rule for nested anchors. Because a link cannot contain a link, the parser partially closes the outer one, inserts the inner one, and then attempts to reopen the outer around whatever content follows. The result is that one anchor in the source becomes two in the DOM. Styling and event listeners that assumed a single element now apply to two, and a click handler attached before the repair may fire for only part of the original region.
Knowing these two rules converts most debugging from guesswork into deduction: if a selector fails or a handler behaves intermittently, inspect whether the tree you wrote is the tree you got.
Cases That Actually Break Layouts
Structural violations in lists and tables do not merely look wrong, they change how the browser builds the box tree, and the styling you wrote stops applying to elements that are no longer where you assumed.
<!-- Illegal: text and a div are not list items. -->
<ul>
Latest posts
<li>First</li>
<div>Second</div>
</ul>
<!-- Legal: each item carries its own content. -->
<ul class="posts">
<li><span class="label">Latest posts</span></li>
<li><a href="/posts/1">First</a></li>
<li><a href="/posts/2">Second</a></li>
</ul>
<!-- Illegal: a row outside a table section. -->
<table>
<tr><td>Stray cell</td></tr>
<tbody>
<tr><td>Real row</td></tr>
</tbody>
</table>
<!-- Legal: rows live in a section, and the caption precedes them. -->
<table>
<caption>Quarterly results</caption>
<thead>
<tr><th scope="col">Quarter</th><th scope="col">Revenue</th></tr>
</thead>
<tbody>
<tr><td>Q1</td><td>1.2M</td></tr>
</tbody>
</table>
The table example deserves a second reading. The stray row is not dropped; the parser wraps it in an implied tbody that your stylesheet never targeted, so a rule written for tbody rows applies to some rows and not others. The caption must also come before any section, because the parser hoists a late caption to the front or discards it depending on where it appeared.
Lists behave similarly. A stray text node between two list items remains in the DOM and participates in layout, which is why an unstyled bullet can appear in the margin of a nested list. Wrapping that heading inside the first item, or moving it outside the list entirely, is the fix, and the choice should be driven by whether the heading belongs to the list for screen-reader navigation.
Interactive and Phrasing Content: The Stricter Half
Interactive elements carry additional constraints that follow from the fact that a user must be able to identify and operate each control unambiguously.
<!-- Legal: phrasing content inside a button. -->
<button type="button">
<svg width="16" height="16" aria-hidden="true"></svg>
Save changes
</button>
<!-- Illegal: a button inside a button. -->
<button type="button">
Save
<button type="button" aria-label="Discard">Ã</button>
</button>
<!-- Legal: separate controls with explicit labels. -->
<div class="row">
<button type="button">Save</button>
<button type="button" aria-label="Discard">Ã</button>
</div>
<!-- Legal: a figure holds flow content plus one caption. -->
<figure>
<img src="chart.png" alt="Revenue by quarter" width="640" height="400">
<figcaption>Revenue climbed steadily through the first half.</figcaption>
</figure>
The forbidden combinations that matter most are an anchor inside an anchor, a button inside a button, and any interactive element inside a label other than its own control. Each of these produces a DOM that the accessibility tree cannot describe cleanly: two focusable elements with the same role and an ambiguous name, or one control whose accessible name contains a second control.
Phrasing content has its own trap, which is the reverse of the paragraph rule. An anchor may not contain block-level content in older specifications, though the current one permits flow content that is not interactive, so a link wrapping an entire card is legal as long as it contains no nested link or button. Browsers implement the current rule, but a validator still flags older doctypes, so check which one your project declares.
Validating Before It Ships
Because the parser repairs everything silently, invalid markup has to be caught by tooling rather than by looking at the page. Three layers cover the realistic failure modes. The editor's built-in checking catches structural mistakes while the file is open. A validator run in continuous integration catches anything that slipped through, including templates that are only instantiated at runtime. And a DOM assertion on the rendered page catches the cases where the source is valid but a client-side render produced a tree the parser would never have built.
A compact assertion worth adding to a smoke test walks the rendered document and reports parents whose children violate a small allow-list: no block elements inside paragraphs, no interactive element inside another interactive element, every list item inside a list, and every table cell inside a row. The list is short, and each entry corresponds to a bug class that has already cost somebody a day.
đĄValidate the rendered tree, not only the template file
Server templates, client components, and rich-text editors each have a chance to produce illegal structure. Asserting on the live DOM after hydration covers all three sources with one check.
What Invalid Nesting Costs Accessibility
The accessibility consequences are subtler than a broken border and more expensive than a console warning. When the parser closes a paragraph early, the text that follows joins a new block, and the reading order a screen-reader user hears no longer matches the visual order on the page. When a caption is hoisted out of its figure, the association between image and description is lost, and the image is announced without context.
Focus order is the other casualty. Controls that were hoisted out of their original container land in a different sequence than the designer intended, so tabbing jumps across the screen between related buttons. Because the visual layout is driven by CSS, which sees the repaired tree, the mismatch between where a control looks and where it sits in the document order is invisible to sighted users.
The cheapest way to hold the line is to write markup that is valid by construction: lists only ever contain items, rows only ever live in table sections, and interactive elements never contain one another. Once those three invariants hold, most of the recovery rules in the parser have nothing to do.
Common Mistakes
Putting a heading or a div directly inside a list
Fix: Wrap it in the first list item, or place it outside the list, so the parser has nothing to repair.
Nesting an anchor inside an anchor to make a card clickable
Fix: Use one link for the card and style the inner text as a visual affordance; nested links are untangled by the parser.
Relying on the browser's recovery to produce the DOM you meant
Fix: Run a validator in CI and assert on the rendered tree, because the repaired structure rarely matches the source.
Assuming invalid markup is only a cosmetic issue
Fix: Repaired trees change reading and focus order, so screen-reader users hear a different document than the one on screen.
Key Takeaways
- The HTML parser repairs invalid nesting silently, so screen inspection can never confirm validity.
- Content models come in two families: flow content for structure and phrasing content for text-level material.
- Lists accept only items, tables have a strict section-and-row hierarchy, and interactive elements may never contain one another.
- A validator catches templates; a DOM assertion catches rendered output; you need both.
- Invalid nesting alters reading and focus order, which makes it an accessibility defect rather than a style problem.