0%
Exam Sidemann Logo Light Mode

HTML Lists

Learn to group and organize related content with ordered, unordered, and nested lists.

Why Do We Need Lists on a Webpage? The "Why" Before the "How"

Imagine you're writing a recipe, a set of instructions, or just a simple shopping list. You wouldn't write it all out in one long, continuous paragraph. That would be confusing and hard to read. Instead, you naturally group related items together, often with bullet points or numbers.

This is exactly why we use lists in HTML. They serve a crucial purpose: to group and organize related content. By using list tags, you are not just making the text look like a list; you are giving it semantic meaning. You are telling the web browser, "Hey, these items here all belong together as part of a single group." This is incredibly important not only for human readers but also for search engines and screen readers (used by visually impaired users) that need to understand the structure and meaning of your content.

So, just like with tables, we use HTML to create the structure of the list and CSS to control its appearance or style.

The Core Building Blocks of HTML Lists

Creating lists is remarkably simple. You only need to know three essential tags:

  • <ul> (Unordered List): This is the container for a list where the order of the items does not matter. Think of a shopping list for groceries—it doesn't matter if you buy the milk before the bread. Browsers, by default, will display these list items with a bullet point (a solid black circle called a "disc").
  • <ol> (Ordered List): This is the container for a list where the sequence of the items is important. Think of a recipe's instructions—you must mix the ingredients before you put the cake in the oven. Browsers will automatically number these items (1, 2, 3, etc.).
  • <li> (List Item): This tag is used for every individual item inside either a <ul> or an <ol>. You can't have an <li> on its own; it must always be a child of a list container.

That's it! With these three tags, you can create almost any list imaginable.

Part 1: The Unordered List <ul> - For When Order Doesn't Matter

Let's start with the most common type of list. You use a <ul> whenever you have a group of items where the sequence is irrelevant.

Example 1: A Simple Shopping List

Let's say you need to buy a few things from the store. Here’s how you would structure that in HTML.

HTML
<h2>My Shopping List</h2>
<ul>
  <li>Milk</li>
  <li>Bread</li>
  <li>Cheese</li>
  <li>Apples</li>
</ul>

Deep Explanation: We start with the <ul> tag. Inside, every single item is wrapped in its own <li> tags. Finally, we close the list with the </ul> tag. The browser sees the <ul> and automatically adds a standard bullet point before each <li> item.

Part 2: The Ordered List <ol> - For When Sequence is Key

Now, let's switch to a scenario where the order is critical. An <ol> is the perfect tool for step-by-step instructions, rankings, or any list that follows a progression.

Example 2: Simple Recipe Instructions

Here are the steps for making a simple cup of tea. The order is very important.

HTML
<h2>How to Make Tea</h2>
<ol>
  <li>Boil water.</li>
  <li>Place the teabag in a cup.</li>
  <li>Pour the boiling water into the cup.</li>
  <li>Let the tea steep for 3-5 minutes.</li>
  <li>Remove the teabag and enjoy.</li>
</ol>

Deep Explanation: This time, we use the <ol> tag to signal that the sequence matters. The browser automatically numbers each <li> item, starting with "1.". If you were to add a new step, the browser would automatically renumber everything for you.

Part 3: Nested Lists - Putting Lists Inside Other Lists

This is where lists become incredibly powerful. You can place an entire list inside of a list item (<li>). This is called "nesting" and it's perfect for creating outlines or sub-categories.

The Golden Rule of Nesting: A new <ul> or <ol> can only be placed inside an <li> element. It cannot be placed directly inside another <ul> or <ol>.

Example 3: A Nested Unordered List

Let's expand our shopping list. Under "Cheese," we want to specify a few different types we could get.

HTML
<h2>My Detailed Shopping List</h2>
<ul>
  <li>Milk</li>
  <li>Bread</li>
  <li>Cheese
    <!-- The nested list starts INSIDE the 'Cheese' li -->
    <ul>
      <li>Cheddar</li>
      <li>Mozzarella</li>
      <li>Gouda</li>
    </ul>
  </li>
  <li>Apples</li>
</ul>

Deep Explanation: To add the sub-list, we go inside the <li> for "Cheese" and start a brand new <ul>. Crucially, we close the inner </ul> before we close the outer </li> for "Cheese." Notice that the browser automatically indents the nested list and changes the bullet point style to show that it's a sub-list.

Example 4: A Complex Nested Ordered and Unordered List

Let's create a project outline that uses both ordered and unordered lists to show its structure.

HTML
<h2>Project Outline</h2>
<ol>
  <li>Phase 1: Research
    <ul>
      <li>Gather user requirements</li>
      <li>Analyze competitor products</li>
    </ul>
  </li>
  <li>Phase 2: Design
    <ul>
      <li>Create wireframes</li>
      <li>Develop mockups
        <!-- We can nest even deeper! -->
        <ol>
          <li>Mobile version</li>
          <li>Desktop version</li>
        </ol>
      </li>
      <li>Finalize branding</li>
    </ul>
  </li>
  <li>Phase 3: Development</li>
</ol>

Part 4: Styling Your Lists with CSS - Changing the Markers

While the default bullets and numbers are useful, they can be plain. With CSS, you have full control over the appearance of the list markers using the list-style-type property. You'll add a <style> block to your HTML and target the <ul> or <ol> you want to change.

Example 5: Styling Unordered List Markers

HTML
<style>
  /* Target the ul with the class "circles" */
  ul.circles {
    list-style-type: circle; /* Hollow circles */
  }

  /* Target the ul with the class "squares" */
  ul.squares {
    list-style-type: square; /* Filled squares */
  }

  /* Target the ul with the class "no-bullets" */
  ul.no-bullets {
    list-style-type: none; /* Removes the bullets completely */
  }
</style>

<h3>Circle Bullets</h3>
<ul class="circles">
  <li>Item A</li>
  <li>Item B</li>
</ul>

<h3>Square Bullets</h3>
<ul class="squares">
  <li>Item X</li>
  <li>Item Y</li>
</ul>

<h3>No Bullets</h3>
<ul class="no-bullets">
  <li>Visible Item 1</li>
  <li>Visible Item 2</li>
</ul>

Example 6: Styling Ordered List Markers

HTML
<style>
  ol.upper-alpha {
    list-style-type: upper-alpha; /* A, B, C... */
  }
  ol.lower-roman {
    list-style-type: lower-roman; /* i, ii, iii... */
  }
</style>

<h3>Project Steps (A, B, C)</h3>
<ol class="upper-alpha">
  <li>First Step</li>
  <li>Second Step</li>
</ol>

<h3>Appendix (i, ii, iii)</h3>
<ol class="lower-roman">
  <li>Reference One</li>
  <li>Reference Two</li>
</ol>

Common list-style-type values for <ol>: decimal, lower-alpha, upper-alpha, lower-roman, upper-roman.

How do I get markers like (a) or -?

This is easily achieved by hiding the default marker and adding your own using CSS pseudo-elements (::before).

HTML
<style>
  ul.hyphen-list {
    list-style-type: none; /* 1. Hide the original bullet */
    padding-left: 1em;     /* Add some space on the left */
  }

  ul.hyphen-list li::before {
    content: "- "; /* 2. Add your custom content before each li */
  }
</style>

<h3>Hyphen List</h3>
<ul class="hyphen-list">
  <li>First item</li>
  <li>Second item</li>
</ul>

So, Where Else Are Lists Used? The Big Secret: Navigation Menus!

You now know how to create content lists, but one of the most common and important uses for HTML lists has nothing to do with bullet points. Nearly every navigation menu on every website is built using a <ul>!

Why? Because a navigation menu is, semantically, just a list of links. You start with a standard <ul>, remove the bullet points with CSS (list-style-type: none;), and then style the list items to appear horizontally.

HTML
<style>
  nav ul {
    list-style-type: none;
    background-color: #333;
    padding: 0;
    margin: 0;
    overflow: hidden;
  }
  nav li {
    float: left; /* Make them sit side-by-side */
  }
  nav li a {
    display: block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
  }
  nav li a:hover {
    background-color: #111;
  }
</style>

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Services</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

By mastering <ul>, <ol>, and <li>, you haven't just learned how to make simple lists—you've unlocked the fundamental technique for structuring content and building the primary navigation for entire websites.

Contextual AI Tutor

Hello! Ask me a question about the section you are reading.
Tables Lists Text Elements Input & Forms Images & Videos HTML Intro