Nested Lists
By the end of this lesson you will nest lists correctly, mix numbered and bulleted levels, number a multi-level outline like 1, 1.1, 1.2, build a menu with a sub-menu, and spot the invalid markup that many beginners write.
The basic idea appeared in Ordered & Unordered Lists. This lesson goes deeper, because nested lists are behind syllabus outlines, site maps, comment threads and almost every navigation menu.
The one rule that matters
A <ul> or <ol> may contain only <li> elements as direct children (plus <script> and <template>). So a sub-list must go inside an <li>, usually after that item's text. The <li> closes only after its sub-list closes.
Think of a train. The list is the train, each <li> is a coach, and a sub-list is a group of passengers inside one coach. Passengers cannot hang between two coaches; they must be inside one.
<!-- Wrong: the inner list sits between two items -->
<ul>
<li>Fruits</li>
<ul>
<li>Mango</li>
</ul>
<li>Vegetables</li>
</ul>
<!-- Right: the inner list belongs to the "Fruits" item -->
<ul>
<li>Fruits
<ul>
<li>Mango</li>
</ul>
</li>
<li>Vegetables</li>
</ul>Both versions look almost the same on screen, which is why the mistake is so common. The browser does not fix the wrong version: it keeps the <ul> as a direct child of the outer <ul>. A validator reports it as an error, and a screen reader may announce the sub-list as a separate list that belongs to no item.
Step 1: a two-level list
Start with a shopping list for a family function in Lucknow. Each shop is an item, and the things to buy there form a sub-list.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Two-level list</title>
</head>
<body>
<h1>Shopping for Rohan's birthday</h1>
<ul>
<li>Sweet shop
<ul>
<li>1 kg motichoor ladoo</li>
<li>Birthday cake</li>
</ul>
</li>
<li>Stationery shop
<ul>
<li>Balloons</li>
<li>Return gifts
<ul>
<li>Colour pencils</li>
<li>Notebooks</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html>You should see a solid round bullet for the shops, a hollow circle for level two and a small square for level three. Browsers change the bullet style for each level of nested <ul> so the levels are easy to tell apart. Nested <ol> lists do not change style by themselves: every level counts 1, 2, 3 unless you set type or CSS.
Step 2: mixing ordered and unordered lists
Each level can be a different kind of list. Ask the usual question at every level: does the order matter here?
<h2>Kochi weekend trip</h2>
<ol>
<li>Saturday
<ul>
<li>Fort Kochi beach</li>
<li>Chinese fishing nets</li>
</ul>
</li>
<li>Sunday
<ol>
<li>Ferry to Mattancherry at 9 am</li>
<li>Lunch at the spice market</li>
<li>Train back at 5 pm</li>
</ol>
</li>
</ol>The days are in order, so the outer list is an <ol>. On Saturday you can see the places in any order, so that sub-list is a <ul>. Sunday's plan is timed, so its sub-list is an <ol> again.
Step 3: a multi-level outline numbered 1.1
Syllabus documents, legal terms and project plans use numbers like 2.1 and 2.1.3. HTML gives you the structure; CSS counters give you the numbers. A counter is a number that CSS keeps and increases for you, like the token counter at a bank that goes up by one for each customer.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Outline numbering</title>
<style>
.outline, .outline ol { counter-reset: item; list-style: none; padding-left: 1.5rem; }
.outline li { counter-increment: item; }
.outline li::before { content: counters(item, ".") ". "; font-weight: bold; }
</style>
</head>
<body>
<h1>Web design course outline</h1>
<ol class="outline">
<li>HTML basics
<ol>
<li>Document structure</li>
<li>Text and lists
<ol>
<li>Ordered lists</li>
<li>Nested lists</li>
</ol>
</li>
</ol>
</li>
<li>CSS basics
<ol>
<li>Selectors</li>
<li>The box model</li>
</ol>
</li>
</ol>
</body>
</html>You should see 1, 1.1, 1.2, 1.2.1, 1.2.2, 2, 2.1 and 2.2. Each <ol> starts a fresh counter called item, each <li> adds one, and counters(item, ".") joins the values from every level with a dot. The HTML stays a plain, valid nested list. If you add a topic, every number updates by itself.
Navigation menus with sub-menus
A site menu with a sub-menu is a nested list of links inside <nav>:
<nav aria-label="Main">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/courses">Courses</a>
<ul>
<li><a href="/courses/class-10">Class 10</a></li>
<li><a href="/courses/class-12">Class 12</a></li>
</ul>
</li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>When the sub-menu is always visible, as in a footer site map, this is all you need. Screen readers announce "list, 3 items" and then the nested list, so users understand the structure.
Drop-down menus that open and close need more care. Opening a sub-menu only on mouse hover locks out keyboard and touch users. The usual accessible pattern adds a <button> with aria-expanded that shows and hides the sub-list. You will build this in Accessible Navigation Systems and Keyboard Navigation & Focus Management.
Accessibility of deep nesting
Screen readers tell users when they enter a list, how many items it has and, in many cases, which nesting level they are on. That is helpful at two or three levels. At five levels, a listener has to remember a long chain of "level 4, item 2 of 3" messages, much like following directions with too many turns.
- Keep to about three levels. If you need more, split the content with headings.
- Never use a list only to indent. An empty
<ul>around a paragraph to push it right confuses screen readers. Use CSSmarginorpadding. - Give each item real text. An item that holds only a sub-list, with no label, leaves the listener asking "a list of what?"
Common mistakes: invalid nesting
| Wrong | What goes wrong | Fix |
|---|---|---|
<ul> directly inside <ul> | Invalid; the sub-list belongs to no item | Move it inside the previous <li> |
</li> closed before the sub-list | Same problem as above | Close </li> after the sub-list |
<div> or <p> directly inside <ul> | Invalid child of a list | Put it inside an <li> or outside the list |
<li> with no <ul> or <ol> around it | Not a list at all | Wrap items in a list element |
| Typing "1.1" into the item text | Numbers go stale when items change | Use CSS counters |
The W3C validator catches the first four in seconds, so run it on any page with complex lists.
Interview questions
1. Where must a nested list be placed?
Inside an <li> of the outer list, usually after that item's text. The only valid direct children of <ul> and <ol> are <li> plus script-supporting elements, so a list placed between two items is invalid.
2. Does the browser correct a <ul> written directly inside another <ul>?
No. The parser keeps the inner <ul> as a direct child of the outer list. It usually looks fine, but it is invalid HTML and the sub-list is not tied to any item for assistive technology.
3. How would you number a nested outline as 1, 1.1, 1.1.1?
Use CSS counters: counter-reset on each <ol>, counter-increment on each <li>, and content: counters(name, ".") in a ::before pseudo-element. The markup stays a normal nested list, and numbers update automatically.
4. What makes a drop-down sub-menu accessible?
It must work with the keyboard and touch, not only mouse hover. A common pattern uses a <button> with aria-expanded to toggle the nested <ul>, keeps a logical focus order and closes with the Escape key.
5. Can you mix <ol> and <ul> when nesting?
Yes. Each level is chosen on its own merits: use <ol> where order matters and <ul> where it does not. A numbered list of days with an unordered list of places under each day is a typical example.
Key points
- A sub-list goes inside an
<li>, and that<li>closes after the sub-list. - The browser does not repair a list placed directly inside another list; validate your pages.
- Choose
<ol>or<ul>separately for each level. - CSS counters with
counters()produce outline numbers such as 1.2.1. - Menus with sub-menus are nested lists of links inside
<nav>; drop-downs need keyboard support. - Keep nesting to about three levels and never use lists just to indent.
Exercise
Build a site map for "Meera's Music School, Pune". The top level has Home, Courses, Events and Contact. Under Courses, add Vocal and Instruments; under Instruments, add Tabla, Sitar and Keyboard. Under Events, add an ordered list of three upcoming concerts in date order. Put it all inside a <nav> with a suitable label, and make every item a link.
Show solution
<nav aria-label="Site map">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/courses">Courses</a>
<ul>
<li><a href="/courses/vocal">Vocal</a></li>
<li><a href="/courses/instruments">Instruments</a>
<ul>
<li><a href="/courses/tabla">Tabla</a></li>
<li><a href="/courses/sitar">Sitar</a></li>
<li><a href="/courses/keyboard">Keyboard</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="/events">Events</a>
<ol>
<li><a href="/events/winter-recital">Winter recital, 6 December</a></li>
<li><a href="/events/new-year-concert">New Year concert, 1 January</a></li>
<li><a href="/events/annual-day">Annual day, 14 February</a></li>
</ol>
</li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>Every sub-list sits inside the <li> it belongs to, and each of those <li> elements closes after its sub-list. The concerts use <ol> because they are in date order; the rest use <ul>.
Quick Quiz
1. Which markup is valid?
The sub-list must be inside the <li> it belongs to.
2. What bullet do browsers usually show for a second-level <ul>?
Default styles use disc, then circle, then square as <ul> lists nest.
3. Which CSS function joins counter values from all nesting levels, as in 2.1.3?
counters(name, ".") joins the values of every nested counter with the given separator.
4. Why is a hover-only drop-down menu a problem?
Sub-menus must also open with the keyboard and touch, usually through a button with aria-expanded.
Learn HTML with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
