Back to HTML

Nesting HTML Elements Correctly

Coding Tutorials Team 30 July 2026

Nesting is the mechanism that turns a flat file into a tree. Get it right and CSS and JavaScript become predictable. Get it wrong and nothing announces the problem, because the parser repairs your markup silently and hands you a structure you did not write.

Elements nest, they never overlap

The rule is that an element opened inside another must be closed before that outer element closes. Think of them as boxes inside boxes rather than as ribbons laid across each other.

<p><strong>Correct</strong> nesting.</p> <p><strong>Overlapping</p></strong>

The second example is invalid. What makes it dangerous rather than merely wrong is that the page will still render, and it may even look identical. Behind the scenes the parser has closed the strong element for you at the paragraph boundary and possibly created a second one afterwards. Your later attempt to select that element will then find something other than what your file appears to contain.

Indentation is how you actually catch this

Indent one level for every level of nesting. This is not decoration and it is not about looking tidy. It is the only practical way to see at a glance whether your tags close in the right order, because a mismatch appears immediately as a line that does not line up with its opening partner.

<ul class="menu"> <li> <a href="/html/">HTML</a> </li> <li> <a href="/css/">CSS</a> </li> </ul>

On a page of any size, unindented markup makes a missing closing tag effectively invisible. Every editor will indent automatically if you let it, and it is worth setting up early rather than after your first hour lost to a stray div.

The content model decides what may go inside what

Correct order is not sufficient. Each element also declares what it is allowed to contain, and this is called its content model. Some of the rules that trip people up regularly:

  • A paragraph may only contain text and inline elements. Putting a div or a heading inside one ends the paragraph at that point.
  • A list may only have list items as direct children. Text or a div placed directly inside a list is invalid, though a list item can then contain anything you like.
  • An anchor must not contain another anchor. Nested links have no sensible meaning and will be pulled apart.
  • A table has a strict structure of rows containing cells. Content placed outside that gets relocated, often to just before the table.
  • A button should not contain interactive elements, because a link inside a button leaves the browser unable to decide which action a click means.

Watch for the paragraph that closes itself

The single most common surprise is this one:

<p> Some introduction. <div>A box</div> More text. </p>

You wrote one paragraph containing a div. What the browser builds is a paragraph containing the introduction, then a sibling div, then loose text, then an empty paragraph created from your orphaned closing tag. A CSS rule styling the div as a child of the paragraph will now never match, and the space between the blocks will not be what you expect. Nothing warns you, because the parser considers this handled.

Block inside inline is usually the underlying mistake

The general pattern behind most invalid nesting is placing a block level element inside an inline one. A span wrapped around several paragraphs is the usual example. The modern specification is more permissive than the old block and inline distinction suggested, and an anchor may now legitimately wrap entire sections, which is how a whole clickable card is built. But a span cannot, and reaching for one to group blocks is a sign that a div is what you actually wanted.

Check the tree, not the file

Because the repairs happen invisibly, reading your own source is not a reliable way to verify structure. Two tools tell you the truth. The W3C validator will list every nesting violation with a line number, and it is worth running on any page that is behaving strangely. The element inspector in your browser shows the DOM that was actually built, so comparing it against what you wrote reveals immediately where the parser stepped in.

Why this is worth the care

Selectors, event handling, inherited styles, screen reader navigation and the reading order of your page all depend on the tree rather than on the file. Correct nesting is what keeps those two things identical, which means the page behaves the way your source suggests it should. That is a small discipline with a large payoff.