Sub topic 16
Nesting lists
Nested lists let you show a hierarchy: categories with items, steps with sub-steps, or a site menu with submenus. This chapter assumes you can write ul, ol, and li elements and that you know how to open an HTML file in a browser. It covers the one rule that makes nesting valid, how each list type behaves when nested, how to build common patterns, and the errors that show up most often.
The core rule: a list goes inside an li
A list element (ul or ol) may only contain li elements as its direct children. A nested list is therefore never placed directly inside its parent list. It goes inside the li of the item it belongs to.
<ul>
<li>Fruit
<ul>
<li>Apples</li>
<li>Pears</li>
</ul>
</li>
<li>Vegetables</li>
</ul>Read this as "Fruit has two sub-items: Apples and Pears." The inner ul belongs to the Fruit item. The text "Fruit" and the inner list share one li, so the browser, and a screen reader, treat them as a single parent item with children.
The most common mistake
Many beginners place the inner list as a sibling of the li elements, directly inside the outer ul:
<ul>
<li>Fruit</li>
<ul>
<li>Apples</li>
<li>Pears</li>
</ul>
<li>Vegetables</li>
</ul>Browsers usually render this with the indentation you wanted, so it looks correct. It is still invalid HTML. The inner list is not attached to any item, so assistive technology cannot tell that Apples and Pears belong to Fruit. Validators report an error, and CSS selectors that rely on the structure, such as li > ul, will not match.
Mixing list types
Nesting is not limited to the same type. You can put an ol inside a ul, a ul inside an ol, or any combination. Choose the type for each level by asking whether the order of the items at that level matters.
<ol>
<li>Prepare the project
<ul>
<li>Create a folder</li>
<li>Create an <code>index.html</code> file</li>
</ul>
</li>
<li>Write the page
<ol>
<li>Add the head</li>
<li>Add the body</li>
</ol>
</li>
<li>Open the file in a browser</li>
</ol>The top level is an ol because the steps happen in sequence. Under step 1 the sub-items are an unordered ul, since the folder and the file can be created in either order. Under step 2 the sub-steps are an ol because the head comes first.
By default, browsers number the nested ol starting again at 1, and they change the bullet style for nested ul levels (usually disc, then circle, then square). These defaults come from the browser's stylesheet, not from the HTML, so you can change them with CSS.
Nesting description lists
A description list (dl) has a different structure: its children are dt and dd elements. A nested list goes inside a dd, because a description can contain other flow content, including lists.
<dl>
<dt>Web languages</dt>
<dd>
<ul>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
</dd>
<dt>Web servers</dt>
<dd>Software that sends pages to browsers.</dd>
</dl>Here the term "Web languages" is described by a list rather than by a sentence. A dt is meant for the term only, so avoid putting lists in it.
Building an outline
Nested lists suit tables of contents. Combined with the fragment links you met earlier, each entry can jump to a section of the page.
<nav aria-label="Table of contents">
<ol>
<li><a href="#links">Links</a>
<ol>
<li><a href="#absolute">Absolute URLs</a></li>
<li><a href="#relative">Relative URLs</a></li>
</ol>
</li>
<li><a href="#images">Images</a></li>
<li><a href="#lists">Lists</a></li>
</ol>
</nav>The link and the inner list live in the same li. The link is the item's label, and the inner ol holds its children. This is the same pattern a dropdown navigation menu uses, which a later chapter builds on.
Controlling numbering in nested ordered lists
A nested ol restarts its count, so you get 1, 2, 3 at each level. To produce legal-style numbering such as 1.1 or 1.2, you need CSS counters, which are outside the scope of this chapter. In plain HTML you can still use the type attribute (1, a, A, i, I) to vary the marker at each level, for example <ol type="a">. Prefer the CSS list-style-type property for styling, and use type only when the marker is part of the meaning of the content, such as a list that other text refers to as "step a".
Accessibility notes
- Screen readers announce the number of items and the nesting level, for example "list with 2 items, nested level 2." This only works when the nesting is valid, which is another reason to keep the inner list inside its
li. - Keep nesting shallow. Three levels is a practical limit for most readers. If you need more, consider splitting the content into separate sections with headings.
- Do not use nested lists purely to indent text. Indentation is a styling concern, so use CSS margins or padding for that.
- Do not use an empty
lito create spacing. Use CSS instead.
Common errors summary
| Mistake | Why it is a problem | Fix |
|---|---|---|
| Inner `ul` placed directly inside the outer `ul` | Invalid HTML, and the inner items are not tied to a parent item | Move the inner list inside the parent `li` |
| Closing the parent `li` before the nested list | The nested list ends up outside the item it describes | Place the closing `</li>` after the nested list |
| Lists nested to five or more levels | Hard to read and hard to navigate with a screen reader | Flatten the structure or split it into sections |
| Using `ul` for steps that must happen in order | The order is not conveyed to assistive technology | Use `ol` for sequences |
| Putting a list inside `dt` | `dt` is for the term, not for content | Put the list in a `dd` |
Check your understanding
Exercise
Look at this markup. What is wrong with it, and how would you fix it? <ul> <li>Drinks</li> <ul> <li>Tea</li> <li>Coffee</li> </ul> <li>Snacks</li> </ul>
Show answer
The inner ul is a direct child of the outer ul, which is invalid because a ul may contain only li elements. Tea and Coffee are not attached to Drinks. Remove the closing </li> after "Drinks", put the inner ul inside that li, then close the li after the inner list: <li>Drinks<ul><li>Tea</li><li>Coffee</li></ul></li>.
Exercise
You are writing a recipe. The ingredients can be gathered in any order, but the cooking steps must be followed in sequence. One cooking step, "Make the sauce", has three sub-steps that must also be done in order. Which list types do you use at each level?
Show answer
Use a ul for the ingredients. Use an ol for the cooking steps. Inside the "Make the sauce" li, nest another ol for its three sub-steps, because their order matters too.
Exercise
In a description list, where does a nested ul belong: inside the dt, inside the dd, or directly inside the dl?
Show answer
Inside the dd. A dl may only contain dt and dd (and div wrappers around them), and a dd can hold lists. A dt is meant to hold the term only.