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:
- The rule of lists: a
ulorolmay only havelielements as direct children. Aulsitting between twolielements breaks that rule. - 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.