Skip to the workspace
Pebellslearn by building
Lists  ›  Lesson

Nested Lists

Nested lists 12 minA list can contain another list, and the inner list always goes inside an li of the outer list, not directly inside the ul or ol.

html / Nested Lists

Explore each scene at your own pace.

01 · The rule

The rule

Nested Lists

A list can contain another list, and the inner list always goes inside an li of the outer list, not directly inside the ul or ol.

Place a nested list inside its parent item and inspect the resulting hierarchy.

A table of contents has chapters with sections inside them. A site menu has categories with pages inside them. A recipe's steps can have sub-steps. These are lists inside lists, and HTML handles them by nesting. There is exactly one rule to get right.

The rule

The inner list lives inside an li, never loose in the outer list.

To nest, put a complete new list inside the li it belongs to, after that item's text and before its closing </li>:

<ul>
  <li>Home</li>
  <li>Courses
    <ul>
      <li>Maths</li>
      <li>Science</li>
    </ul>
  </li>
  <li>Contact</li>
</ul>

Why inside the li? Two reasons:

  1. The rule of lists: a ul or ol may only have li elements as direct children. A ul sitting between two li elements breaks that rule.
  2. The meaning: Maths and Science belong to Courses. Putting their list inside the Courses item says exactly that, and screen readers announce the nesting level so users know where they are.

You can mix types. An ordered list of steps can hold an unordered list of options within one step:

<ol>
  <li>Choose a base:
    <ul>
      <li>Rice</li>
      <li>Millet</li>
    </ul>
  </li>
  <li>Add the vegetables.</li>
</ol>

Nesting can go deeper, but more than two or three levels usually means the content needs rethinking or splitting into sections with headings.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.