Skip to content

HTML Tutorial Elements and Attributes

Nested HTML Elements: Putting Elements Inside Elements

Lesson 6 of 81 2 min read By Deepak Paswan Updated Oct 4, 2026

Most pages are built by putting elements inside other elements. This is called nesting.

index.html
HTML
<body>
  <h1>Juniper Books</h1>
  <p>Open <strong>every day</strong>.</p>
</body>

Here h1 and p are inside body. The strong element is inside the p. We say body is the parent and h1 and p are its children. Elements with the same parent are siblings.

The family tree

You can draw any page as a tree:

  • html
  • head
  • body
  • h1
  • p
  • strong

The browser builds this same tree in memory. It is called the DOM, and CSS and JavaScript both use it to find the elements you want to change. You will use the tree idea again and again.

The nesting rule

Close elements in the reverse order you opened them. The last one opened is the first one closed.

index.html
HTML
<!-- Correct -->
<p>This is <strong>important</strong> news.</p>

<!-- Wrong: tags cross over -->
<p>This is <strong>important</p></strong>
warning
Crossed tags
In the wrong example the strong opens inside the paragraph but closes outside it. Browsers will try to repair it, but the result can differ between browsers. Keep every element fully inside its parent.

Some elements have limits

Not every element can go inside every other. A paragraph cannot contain another paragraph or a heading. A list item belongs inside a list. You will learn the rules as you meet each element, and a validator (see Validating and Debugging HTML) will tell you when you break one.

lightbulb
Indent to see the tree
Indent children by two spaces under their parent. If the indentation looks odd, the nesting probably is too.