Table of contents
1.
Introduction
2.
Lists in HTML
2.1.
1. Ordered Lists
2.2.
2. Unordered Lists
2.3.
3. Definition Lists
3.
Basic Nested List
3.1.
Example of a multi-level nested list
3.2.
Nesting in Definition Lists
3.3.
Nested Unordered List in HTML
3.3.1.
How to Create a Nested Unordered List
3.3.2.
Example
3.4.
Nested Ordered List in HTML
3.4.1.
How to Create a Nested Ordered List
3.4.2.
Example
4.
Styling Nested Lists with CSS
4.1.
1. Indentation & Styling
4.2.
2. Customizing List Styles
4.3.
Example of Nested List
5.
Applications of Nested Lists
6.
Key Differences Between Nested Unordered and Ordered Lists
7.
Best Practices for Utilizing Nested Lists
8.
Browser Compatibility
9.
Frequently Asked Questions
9.1.
What is the difference between <ul> and <ol> in HTML?
9.2.
Can I nest an unordered list inside an ordered list?
9.3.
How can I style nested lists using CSS?
10.
Conclusion
Last Updated: Jan 3, 2025
Easy

Nested List in HTML

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The nested list in HTML is a structure where a list is placed inside another list, allowing developers to create hierarchical or multi-level lists. This approach is commonly used to represent categories and subcategories, menu options, or any content that requires a tree-like structure. Nested lists can be created using both ordered (<ol>) and unordered (<ul>) list elements, making them versatile for various use cases.

Nested List in HTML

In this article, we’ll discuss nested unordered lists and nested ordered lists in HTML. 

Lists in HTML

HTML provides three main types of lists: ordered lists, unordered lists, & definition lists. Each type has a specific purpose & can be used to organize content effectively.

1. Ordered Lists

Ordered lists, denoted by the <ol> tag, are used when the order of items is important. Each item in an ordered list is automatically numbered, making it easy to convey a sequence or ranking. The <li> tag is used to represent individual list items within the <ol> tag.

Example:

<ol>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
</ol>

2. Unordered Lists

Unordered lists, represented by the <ul> tag, are used when the order of items is not significant. Each item in an unordered list is typically preceded by a bullet point. Like ordered lists, the <li> tag is used to denote each list item within the <ul> tag.

Example:

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

3. Definition Lists

Definition lists, created using the <dl> tag, are used to present a list of terms & their corresponding definitions. The <dt> tag is used for the term, while the <dd> tag is used for the definition.

Example:

<dl>
  <dt>Term 1</dt>
  <dd>Definition 1</dd>
  <dt>Term 2</dt>
  <dd>Definition 2</dd>
</dl>

Basic Nested List

Nested lists allow you to create hierarchical structures by placing one list inside another. To create a nested list, simply include the child list within a list item (<li>) of the parent list.

Example:

<ul>
  <li>Item 1</li>
  <li>Item 2
    <ul>
      <li>Sub-item 2.1</li>
      <li>Sub-item 2.2</li>
    </ul>
  </li>
  <li>Item 3</li>
</ul>


In this example, we have an unordered list with three items. The second item contains a nested unordered list with two sub-items. This structure visually indicates that the sub-items are related to & fall under the parent item.

You can nest lists multiple levels deep, allowing for complex hierarchies. However, it's important to keep the nesting levels reasonable to maintain readability.

Example of a multi-level nested list

<ol>
  <li>First level
    <ul>
      <li>Second level
        <ol>
          <li>Third level</li>
          <li>Third level</li>
        </ol>
      </li>
      <li>Second level</li>
    </ul>
  </li>
  <li>First level</li>
</ol>


In this example, we have an ordered list at the first level, an unordered list at the second level, & another ordered list at the third level. This demonstrates how different types of lists can be nested within each other to create intricate structures.

Nesting in Definition Lists

Definition lists can also accommodate nested lists, allowing you to provide more detailed or structured information within a definition.

Example:

<dl>
  <dt>Term 1</dt>
  <dd>Definition 1
    <ul>
      <li>Additional point 1</li>
      <li>Additional point 2</li>
    </ul>
  </dd>
  <dt>Term 2</dt>
  <dd>Definition 2
    <ol>
      <li>Step 1</li>
      <li>Step 2</li>
    </ol>
  </dd>
</dl>


In this example, the first term has a definition that includes an unordered list with two additional points. The second term's definition contains an ordered list outlining steps related to the term.

Note: Nesting lists within definition lists allows you to provide more comprehensive explanations or break down complex concepts into smaller, more manageable parts.

Nested Unordered List in HTML

An unordered list displays items using bullet points. When you nest unordered lists, you add a list inside another list item, creating a multi-level structure.

How to Create a Nested Unordered List

Nested unordered lists are created using the <ul> tag for each level. Sub-lists are added inside <li> elements of the parent list.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nested Unordered List</title>
</head>
<body>
    <h1>Nested Unordered List Example</h1>
    <ul>
        <li>Fruits
            <ul>
                <li>Apple</li>
                <li>Banana</li>
                <li>Cherry</li>
            </ul>
        </li>
        <li>Vegetables
            <ul>
                <li>Carrot</li>
                <li>Potato</li>
                <li>Spinach</li>
            </ul>
        </li>
        <li>Dairy</li>
    </ul>
</body>
</html>

 

Output

Output

Explanation

  • The outer list is created with the <ul> tag.
     
  • Each list item (<li>) can contain a nested <ul> to create sub-lists.
     
  • For instance, under "Fruits," a sub-list contains "Apple," "Banana," and "Cherry."
     

This hierarchical structure is easy to read and understand.

Nested Ordered List in HTML

Ordered lists display items with numbers, letters, or Roman numerals. Nested ordered lists use the same <ol> tag but include sub-lists within list items.

How to Create a Nested Ordered List

Nested ordered lists are similar to unordered ones but use the <ol> tag. Sub-lists are added inside <li> elements.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nested Ordered List</title>
</head>
<body>
    <h1>Nested Ordered List Example</h1>
    <ol>
        <li>Web Development
            <ol>
                <li>Frontend
                    <ol>
                        <li>HTML</li>
                        <li>CSS</li>
                        <li>JavaScript</li>
                    </ol>
                </li>
                <li>Backend
                    <ol>
                        <li>Node.js</li>
                        <li>Python</li>
                        <li>PHP</li>
                    </ol>
                </li>
            </ol>
        </li>
        <li>Data Science
            <ol>
                <li>Data Analysis</li>
                <li>Machine Learning</li>
                <li>Deep Learning</li>
            </ol>
        </li>
    </ol>
</body>
</html>


Output

Output

Explanation

  • The outer list is an <ol> with items like "Web Development" and "Data Science."
     
  • Sub-lists for "Frontend" and "Backend" use nested <ol> elements.
     
  • Further levels of nesting (e.g., "HTML," "CSS," and "JavaScript") create a deeper hierarchy.
     

This layout is perfect for step-by-step instructions or hierarchies.

Styling Nested Lists with CSS

CSS helps you to customize the appearance of nested lists to match your website's design & improve readability.

1. Indentation & Styling

By default, nested lists are indented to visually differentiate them from their parent lists. You can control the indentation using the `padding-left` property on the nested list or the `margin-left` property on the list items.

Example:

ul {
  padding-left: 20px;
}

ul ul {
  padding-left: 40px;
}

ol {
  margin-left: 30px;
}


In this example, the first-level unordered list has a left padding of 20 pixels, while the second-level unordered list has a left padding of 40 pixels. The ordered list has a left margin of 30 pixels.

You can also apply other styling properties, such as `color`, `font-size`, or `background-color`, to enhance the appearance of your nested lists.

2. Customizing List Styles

CSS provides the `list-style-type` property to customize the bullet points or numbering style of lists.

Example:

ul {
  list-style-type: circle;
}
ul ul {
  list-style-type: square;
}
ol {
  list-style-type: upper-roman;
}


In this example, the first-level unordered list uses circular bullet points, while the second-level unordered list uses square bullet points. The ordered list uses uppercase Roman numerals.

You can also use custom images as bullet points by using the `list-style-image` property.

Example:

ul {
  list-style-image: url('bullet.png');
}


This code sets a custom image (`bullet.png`) as the bullet point for the unordered list.

Example of Nested List

Let's take a look at a practical example of a nested list that represents a website's navigation menu:

<ul class="menu">
  <li><a href="#">Home</a></li>
  <li><a href="#">Products</a>
    <ul>
      <li><a href="#">Category 1</a></li>
      <li><a href="#">Category 2</a>
        <ul>
          <li><a href="#">Subcategory 1</a></li>
          <li><a href="#">Subcategory 2</a></li>
        </ul>
      </li>
      <li><a href="#">Category 3</a></li>
    </ul>
  </li>
  <li><a href="#">About Us</a></li>
  <li><a href="#">Contact</a></li>
</ul>


In this example, we have a top-level unordered list with class "menu". Each list item represents a main navigation item. The "Products" item contains a nested unordered list representing product categories. The "Category 2" item further nests another unordered list for subcategories.

To style this nested list as a navigation menu, we can use CSS:

.menu {
  list-style-type: none;
  padding: 0;
}
.menu li {
  display: inline-block;
  position: relative;
  padding: 10px;
}
.menu ul {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  background-color: #f9f9f9;
  padding: 0;
}
.menu li:hover > ul {
  display: block;
}
.menu ul li {
  display: block;
}


This CSS code removes the default bullet points, displays the top-level menu items inline, and positions the nested submenus absolute to their parent items. The submenus are hidden by default and only displayed when the parent item is hovered over.

Applications of Nested Lists

Nested lists have various applications in web development. Some common use cases are:

1. Table of Contents: Nested lists can be used to create a hierarchical table of contents for a long document or article. Each main section can be represented as a top-level list item, with subsections nested within them.
 

2. Sitemap: A website's sitemap can be effectively represented using nested lists. The top-level list items represent the main pages, while nested lists represent subpages or categories.
 

3. Outline or Summary: Nested lists are useful for creating outlines or summaries of a document, presentation, or project. The main points can be listed as top-level items, with supporting details or subtopics nested beneath them.
 

4. Cascading Menus: As demonstrated in the previous example, nested lists are commonly used to create cascading or dropdown menus in website navigation. The nested structure allows for organizing menu items into categories and subcategories.
 

5. Threaded Comments: In online forums or blog comments, nested lists can be used to represent the hierarchy of comments and replies. Each comment can be a top-level list item, with replies nested below it, creating a visually organized discussion thread.


6. File and Folder Structure: Nested lists can represent a file and folder structure, with folders as top-level items and files or subfolders nested within them. This can be useful for displaying a directory tree or file explorer interface.

Key Differences Between Nested Unordered and Ordered Lists

ParametersNested Unordered ListNested Ordered List
Bullet PointsYesNo
NumberingNoYes
Use CaseGeneral data groupingSequential or ranked data

Best Practices for Utilizing Nested Lists

To make the most effective use of nested lists, there are certain practices which we can follow, like:


1. Use meaningful hierarchy: Ensure that the nesting levels represent a logical hierarchy of information. Each level should be related to its parent and provide additional detail or context.


2. Keep nesting levels shallow: Avoid excessive nesting levels, as they can make the content harder to read and understand. Aim for a maximum of three to four levels deep, unless absolutely necessary.


3. Use consistent styling: Apply consistent styling to nested lists throughout your website. Use CSS to control indentation, bullet points, and other visual aspects to maintain a cohesive design.


4. Provide visual cues: Use indentation, bullet points, or other visual cues to clearly distinguish between different nesting levels. This helps users quickly grasp the hierarchical structure of the content.


5. Ensure accessibility: Make sure your nested lists are accessible to users with disabilities. Use appropriate HTML tags (`<ul>`, `<ol>`, `<li>`) and provide alternative text for any custom bullet point images.


6. Consider responsive design: When designing for different screen sizes, ensure that your nested lists adapt gracefully. Use CSS media queries to adjust the layout, font sizes, and spacing for optimal readability on various devices.


7. Use semantic markup: Use the appropriate HTML tags for your lists (`<ul>` for unordered lists, `<ol>` for ordered lists, `<dl>` for definition lists) to provide semantic meaning to your content. This improves accessibility and search engine optimization.


8. Test and iterate: Test your nested lists on different browsers, devices, and screen sizes to ensure they display correctly and provide a good user experience. Iterate and refine your design based on feedback and usability testing.

Browser Compatibility

Nested lists are widely supported by all modern web browsers, including Chrome, Firefox, Safari, and Internet Explorer (version 9 and above). However, it's important to ensure that your CSS styles are compatible with the browsers you target.

Some older browsers may have different default styles for nested lists, so it's a good practice to reset or normalize the default styles using CSS to achieve consistent rendering across browsers.

Frequently Asked Questions

What is the difference between <ul> and <ol> in HTML?

<ul> is used for unordered lists with bullet points, while <ol> is for ordered lists with numbers or letters.

Can I nest an unordered list inside an ordered list?

Yes, you can mix and nest lists. For example, an ordered list can have a list item with a nested unordered list inside it.

How can I style nested lists using CSS?

You can style nested lists by targeting specific levels using CSS selectors like ul ul or ol ol. 

Conclusion

Nested lists in HTML allow developers to represent structured and hierarchical data effectively. Unordered lists use bullet points for general grouping, while ordered lists employ numbering for sequential data. By understanding how to create and style nested lists, you'll be better equipped to design clear, organized web pages. 

You can also check out our other blogs on Code360.

Live masterclass