Lesson 15 of 36
HTML Tutorial

Nested Lists

Build multi-level lists and menus by placing a list inside a list item - correctly.

3 min readBeginnerWeb FundamentalsHTML Lists

Real content is often hierarchical: a menu with categories and dishes, a navigation with dropdown pages, an outline with sub-points. For this you nest lists, putting one list inside another.

The golden rule#

A nested list goes inside an <li>, never directly inside the parent <ul> or <ol>. The inner list belongs to the item it describes.

html
<h2>Brew and Byte menu</h2>
<ul>
  <li>Hot drinks
    <ul>
      <li>Espresso</li>
      <li>Latte</li>
      <li>Masala tea</li>
    </ul>
  </li>
  <li>Cold drinks
    <ul>
      <li>Iced coffee</li>
      <li>Lemon soda</li>
    </ul>
  </li>
  <li>Snacks</li>
</ul>

Read it as: the item "Hot drinks" contains a list of drinks. The closing </li> comes after the inner list.

Mixing list types#

You can mix ordered and unordered lists freely. A recipe, for instance, may have ordered steps with unordered notes under some of them:

html
<ol>
  <li>Prepare the base
    <ul>
      <li>Toast the bread</li>
      <li>Rub with garlic</li>
    </ul>
  </li>
  <li>Add the toppings</li>
  <li>Serve warm</li>
</ol>

Browsers automatically vary the default bullet style at deeper levels (disc, circle, square). With CSS you can change that.

Nested navigation#

Dropdown-style menus on many sites are nested lists under the hood, with CSS hiding the inner list until the parent is hovered or focused.

html
<nav aria-label="Main">
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="menu.html">Menu</a>
      <ul>
        <li><a href="menu.html#drinks">Drinks</a></li>
        <li><a href="menu.html#food">Food</a></li>
      </ul>
    </li>
    <li><a href="contact.html">Contact</a></li>
  </ul>
</nav>

Keep accessibility in mind: if a submenu appears on hover only, keyboard users cannot reach it. Make sure it also opens on focus, or keep your navigation simple.

Keep it shallow#

Two or three levels are comfortable for readers. Deeper nesting becomes hard to follow and hard to navigate with a screen reader. If your list keeps growing, consider splitting it into several sections with headings instead.

Common mistakes#

  • Placing <ul> directly inside another <ul> (outside any <li>). It might look fine in the browser, but it is invalid.
  • Forgetting the closing </li> for the parent item.
  • Nesting too deep.
  • Using spaces or dashes to fake indentation instead of real nested lists.
  • Creating hover-only submenus that keyboard users cannot open.

Try it yourself#

Write a nested outline of your cafe or portfolio site: three top-level pages, with two or three sub-pages under at least two of them. Turn the top level into links inside a nav. Validate your markup by checking that every inner list sits within an li, and that indentation lines up with your tags.

Recap#

Nest a list by placing it inside the li it belongs to. Mix ordered and unordered types as the content needs, keep depth to two or three levels, and watch your closing tags.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-