Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

The Shape of an HTML Document · Lesson 9 of 18

Nesting Elements Correctly

A free lesson from Swipe Coding · HTML, level 1 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

Elements can contain other elements, and this is called nesting. The rule is simple but important: whichever element you open last must be the one you close first — nested tags have to close in the reverse order they opened.

<p>This is <strong>very important</strong> text.</p>
<strong> opens after <p> and closes before </p> — correctly nested.
🤖
Crossed tags are the HTML equivalent of tangled shoelaces. 👟

Tags that cross each other instead of nesting properly are invalid and can cause a browser to render the page in unexpected ways, even if it technically still displays something.

<p>This is <strong>incorrectly nested</p></strong>
Invalid — <strong> opened after <p> but closes after </p> instead of before it.

A good habit for avoiding nesting mistakes is indenting nested elements consistently as you write them, the way the examples in this track do. It costs nothing and makes crossed or missing tags far easier to spot at a glance, especially once a page has dozens of elements in it.

Quick check

What is the correct rule for closing nested HTML tags?

Fill in the blank

Elements containing other elements is called ____.

True or false

<p>Some <strong>text</p></strong> is correctly nested HTML.

Keep your streak going in Swipe Coding

Swipe through every level of HTML on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →