Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 122 ยท Lists

Nested Lists

Learn how to nest HTML lists correctly inside li elements, build outlines, mix ordered and unordered lists, number items 1.1 and 1.2, and avoid common mistakes.

Real information is often layered. A course has chapters, and chapters have topics. A menu has categories, and categories have dishes. A to-do item has sub-tasks. When a list item has its own list of items, you nest one list inside another.

Nesting is simple once you know the one rule that matters, and that rule is also where most beginners slip. This lesson shows the correct structure, how to build outlines with different numbering at each level, how to mix ordered and unordered lists, and the mistakes to watch for.

The rule: the sub-list goes inside an <li>

A nested list belongs to a particular item, so it goes inside that item's <li>, after the item's own text:

A two-level list

Look closely at where each </li> is: the "Fruit" item closes after its sub-list. The sub-list is part of the Fruit item, so screen readers announce it as a list within that item, and the structure matches what you see.

Notice the bullets, too. Browsers change the default marker for each level of a nested <ul>: a filled disc, then a hollow circle, then a square. You didn't write any CSS for that.

Indent your code to match the structure. Each level of nesting gets two more spaces. When the indentation is consistent, a misplaced closing tag stands out immediately.

The most common mistake

Here is the version many people write first:

HTML
<!-- Wrong: the <ul> is a direct child of another <ul> -->
<ul>
  <li>Fruit</li>
  <ul>
    <li>Apples</li>
    <li>Pears</li>
  </ul>
  <li>Vegetables</li>
</ul>

It looks almost the same in the browser, which is why the mistake is so common. But it is invalid: the only children allowed in a <ul> or <ol> are <li> elements. The inner list is now an orphan that belongs to no item. Assistive technology may announce the wrong item counts, and CSS that targets "the list inside an item" won't match it.

The fix is to move the closing </li> of "Fruit" below the inner list:

HTML
<!-- Right -->
<ul>
  <li>Fruit
    <ul>
      <li>Apples</li>
      <li>Pears</li>
    </ul>
  </li>
  <li>Vegetables</li>
</ul>

An HTML validator catches this mistake every time, and the browser's DevTools show you where each list really begins and ends.

Mixing ordered and unordered lists

Each level can be whichever list type fits its content. A numbered recipe might have unordered options inside a step, or a bulleted overview might contain numbered instructions:

Steps with options inside

The outer steps happen in order, so they use <ol>. The ingredients in step 1 can be mixed in any order, so they use <ul>. Choose each level on its own merits, exactly as you learned in Lists.

Building an outline

Outlines, such as an essay plan or a table of contents, are nested ordered lists. The type attribute from Ordered List Options lets each level use its own numbering style, a classic outline pattern:

A classic outline

Each nested <ol> starts counting from 1 again (or A, or i). The numbering doesn't continue from the parent level; every list counts its own items.

Numbering like 1.1, 1.2, 1.3

Legal documents and technical manuals often number sub-items with the parent's number in front: 2.1, 2.2, 2.3. HTML doesn't do that on its own, but CSS counters can. The markup stays a plain nested <ol>:

Section numbers with CSS counters

counter-reset starts a fresh counter on every list, counter-increment adds one for each item, and the counters() function joins the values from all the levels with a dot. Add an item anywhere and every number updates. You will learn much more CSS in the Adding CSS to HTML lesson.

Styling nested levels

To style one level differently, use a descendant selector that matches the depth you want:

CSS
/* Every list inside a list item */
li > ul,
li > ol {
  margin-top: 4px;
  margin-bottom: 8px;
}

/* Only the second level of a menu */
.menu ul ul {
  font-size: 0.9em;
  list-style-type: "โ€“ ";
}

The rule li > ul only works when the sub-list really is inside an <li>, one more reason to nest correctly.

Keep it shallow

Nesting is powerful, but deep lists are hard to read and hard to navigate with a screen reader, which announces every level as you move in and out. As a guideline:

  • Two levels are easy to follow.
  • Three levels are fine for outlines and tables of contents.
  • Four or more usually means the content wants headings and separate sections instead.

Nesting is for structure, not indentation. Don't wrap content in extra <ul> elements just to push it to the right. Use CSS margin or padding for that, and keep lists for real lists.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should a nested ul go?

    A sub-list belongs to one item, so it must sit inside that item's <li>. A <ul> directly inside a <ul> is invalid.
  2. 2. In a nested ordered list, what number does the first item of an inner ol show by default?

    Each list counts its own items from 1. For numbers like 2.1, use CSS counters.
  3. 3. A recipe has numbered steps, and one step lists ingredients that can be added in any order. Which structure fits?

    The steps are ordered, so the outer list is an <ol>. The ingredients have no order, so the inner list is a <ul>.
  4. 4. Which CSS function produces numbers like 2.1 and 2.1.3 for nested lists?

    counters() joins the values of all nested counters with the separator you give it. counter() only shows the innermost value.

Summary

  • A nested list goes inside an <li>, after the item's own text; the </li> closes after the sub-list.
  • A <ul> or <ol> placed directly inside another list is invalid, even if it looks right.
  • Each level can be <ul> or <ol>, chosen by whether the order matters at that level.
  • Use the type attribute for classic outlines, and CSS counters for numbers like 1.1 and 1.2.
  • Keep nesting shallow, and never nest lists just for indentation.

You now know every kind of HTML list. Next up, a new chapter: Tables, for data with rows and columns.