Menu
CoddyTech

Nested Lists

Part of the Fundamentals section of Coddy's HTML journey. Lesson 14 of 60.

You can place one list inside another. This is called a nested list. This is useful for creating hierarchical structures, such as outlines or multi-level menus. 

To create a nested list, you simply place an <ul> or <ol> element inside an <li> element of another list.

Here's an example of a nested unordered list:

<ul>
  <li>Item 1</li>
  <li>Item 2
    <ul>
      <li>Subitem 2.1</li>
      <li>Subitem 2.2</li>
    </ul>
  </li>
  <li>Item 3</li>
</ul>

The second item contains a nested list with two subitems. In the browser, it will display like this:

  • Item 1
  • Item 2
    • Subitem 2.1
    • Subitem 2.2
  • Item 3

The sub-list must go inside the <li>, before its closing </li>. A list placed after </li> sits directly inside <ul>, which may only contain <li> items:

<ul>
  <li>Item 2</li>
  <ul>   <!-- wrong: not inside an li -->
    <li>Subitem 2.1</li>
  </ul>
</ul>

Browsers usually draw both versions the same way, so the mistake is easy to miss, but the HTML is invalid, and checks that read the page's structure will fail.

You can also nest ordered lists or mix both. 

challenge icon

Challenge

Easy

Create an HTML document with a nested list. The list should look like this:

  • Fruits
    1. Apple
    2. Banana
    3. Orange
  • Vegetables
    • Carrot
    • Broccoli
    • Spinach
  • Grains

The outer list is already in the starter code. Inside the Fruits item, add a numbered list (<ol>) of Apple, Banana and Orange; inside the Vegetables item, a bulleted list (<ul>) of Carrot, Broccoli and Spinach. Each sub-list goes before that item's closing </li>.

Try it yourself

<!DOCTYPE html>
<html>
    <body>
        <ul>
            <li>Fruits
                <!-- Add the numbered list (Apple, Banana, Orange) here, before </li> -->
            </li>
            <li>Vegetables
                <!-- Add the bulleted list (Carrot, Broccoli, Spinach) here, before </li> -->
            </li>
            <li>Grains</li>
        </ul>
    </body>
</html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Web playground