Chapter 09 of 26

Lists

Lists are everywhere: a syllabus, the steps of an algorithm, a shopping cart, a glossary of terms, even the navigation menu at the top of most websites. HTML has three kinds of lists, and choosing the right one is about one simple question: does the order matter?

In this chapter, we'll learn unordered lists, ordered lists (and their numbering options), description lists, how to nest lists correctly, and how lists are used for navigation menus.

The Three Types of Lists

ListTagsUse when…Default look
Unordered list<ul> + <li>Order doesn't matterBullets (•)
Ordered list<ol> + <li>Order matters (steps, rankings)Numbers (1, 2, 3)
Description list<dl> + <dt> + <dd>Term–description pairsTerms with indented descriptions

Unordered Lists: <ul>

An unordered list (<ul>) is a collection of items where the order does not change the meaning. Each item is a list item (<li>).

<h2>Things to bring to the lab</h2>
<ul>
  <li>College ID card</li>
  <li>Record notebook</li>
  <li>Pen drive</li>
</ul>

Output in the browser:

Things to bring to the lab
  • College ID card
  • Record notebook
  • Pen drive

Swapping “Record notebook” and “Pen drive” doesn't change the meaning, so <ul> is right. Browsers indent the list (40px of left padding by default) and draw a bullet called a marker before each item.


Ordered Lists: <ol>

An ordered list (<ol>) is a list where the sequence matters. The browser numbers the items automatically.

<h2>Steps to compile a C program</h2>
<ol>
  <li>Write the code in hello.c</li>
  <li>Run gcc hello.c -o hello</li>
  <li>Run ./hello</li>
</ol>

Output:

Steps to compile a C program
  1. Write the code in hello.c
  2. Run gcc hello.c -o hello
  3. Run ./hello

Notice that we didn't type the numbers. If you insert a new step in the middle, every number after it updates automatically — that's the advantage over typing “1.”, “2.” by hand.

Changing the Numbering: type

The type attribute picks the kind of counter:

typeCounterExample
1 (default)Decimal numbers1, 2, 3, 4
aLowercase lettersa, b, c, d
AUppercase lettersA, B, C, D
iLowercase Roman numeralsi, ii, iii, iv
IUppercase Roman numeralsI, II, III, IV
<p>Answer any two:</p>
<ol type="a">
  <li>Explain the OSI model.</li>
  <li>Compare TCP and UDP.</li>
  <li>What is subnetting?</li>
</ol>

Output:

Answer any two:
  a. Explain the OSI model.
  b. Compare TCP and UDP.
  c. What is subnetting?

Use type when the counter style is meaningful — for example, a legal document or question paper that refers to “part (b)”. If you only want a different look, CSS list-style-type is the better tool.

Starting From a Different Number: start

start sets the first number. It is always written as a normal integer, even when type is letters or Roman numerals.

<ol start="4" type="I">
  <li>Process scheduling</li>
  <li>Deadlocks</li>
</ol>

Output:

  IV. Process scheduling
   V. Deadlocks

This is useful when a list is split by a paragraph or an image and needs to continue its numbering.

Counting Down: reversed

reversed is a boolean attribute that makes the list count down — perfect for a “Top 3” countdown.

<ol reversed>
  <li>Bubble sort</li>
  <li>Merge sort</li>
  <li>Quick sort</li>
</ol>

Output:

  3. Bubble sort
  2. Merge sort
  1. Quick sort

The items stay in the order you wrote them; only the numbers are reversed.

Setting One Item's Number: value

An <li> inside an <ol> can have a value attribute. That item gets this number, and the following items continue from it.

<ol>
  <li>Arrays</li>
  <li value="10">Graphs</li>
  <li>Tries</li>
</ol>

Output:

   1. Arrays
  10. Graphs
  11. Tries

Description Lists: <dl>, <dt>, <dd>

A description list (<dl>) contains groups of terms (<dt>) and their descriptions (<dd>). It is ideal for glossaries, FAQs and metadata such as “Author: … / Published: …”.

<dl>
  <dt>RAM</dt>
  <dd>Random Access Memory. Volatile: its contents are lost when power is off.</dd>

  <dt>ROM</dt>
  <dd>Read-Only Memory. Non-volatile: keeps its contents without power.</dd>

  <dt>Cache</dt>
  <dd>Small, very fast memory between the CPU and RAM.</dd>
</dl>

By default, browsers show each term on its own line and indent its description underneath (a 40px left margin on <dd>):

RAM
        Random Access Memory. Volatile: its contents are lost when power is off.
ROM
        Read-Only Memory. Non-volatile: keeps its contents without power.
Cache
        Small, very fast memory between the CPU and RAM.

A group can have several terms or several descriptions:

<dl>
  <dt>LIFO</dt>
  <dt>Last In, First Out</dt>
  <dd>The order used by a stack.</dd>

  <dt>Authors</dt>
  <dd>Ritu Sharma</dd>
  <dd>Arjun Rao</dd>
</dl>

For styling, HTML also allows each <dt>/<dd> group to be wrapped in a <div> directly inside the <dl>.

Old tutorials sometimes call <dl> a “definition list”. The current HTML standard calls it a description list, because it isn't only for definitions.


Nesting Lists

Lists can contain other lists, which is how you build outlines like a syllabus. The rule that trips up many beginners:

A nested list must go inside an <li>, not directly inside the outer <ul> or <ol>.

<h2>Semester 3 Syllabus</h2>
<ol>
  <li>Data Structures
    <ul>
      <li>Stacks</li>
      <li>Queues</li>
      <li>Trees</li>
    </ul>
  </li>
  <li>Operating Systems
    <ul>
      <li>Processes</li>
      <li>Memory management</li>
    </ul>
  </li>
</ol>

Output:

Semester 3 Syllabus
  1. Data Structures
       ◦ Stacks
       ◦ Queues
       ◦ Trees
  2. Operating Systems
       ◦ Processes
       ◦ Memory management

Notice the closing </li> of “Data Structures” comes after the inner </ul>. The sub-list is part of that item. Also notice that browsers automatically change the bullet style for nested <ul>s: a filled disc (•) at the first level, a hollow circle (◦) at the second, and a filled square (▪) at the third and deeper.

The Wrong Way

<ul>
  <li>Data Structures</li>
  <ul>
    <li>Stacks</li>
  </ul>
</ul>

It may look fine in the browser, but it is invalid HTML. A <ul> or <ol> may only contain <li> elements (plus <script> and <template>). Saving just this snippet as wrong-list.html and running npx html-validate wrong-list.html gives:

wrong-list.html
  3:4  error  <ul> element is not permitted as content under <ul>  element-permitted-content

✖ 1 problem (1 error, 0 warnings)

The same rule means you can't put a <p> or <h3> directly in a list — put it inside an <li> instead. (An <li> may contain almost anything: paragraphs, images, links, even whole sub-lists.)


Lists for Navigation Menus

A website's menu is really just a list of links, so it is usually marked up as a <ul> inside a <nav> element. Screen readers then announce something like “navigation, list, 4 items”, which tells the user how big the menu is before they explore it.

<nav>
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="tutorials.html">Tutorials</a></li>
    <li><a href="gate.html">GATE</a></li>
    <li><a href="contact.html">Contact</a></li>
  </ul>
</nav>

By itself this shows a plain bulleted list of links. CSS turns it into a horizontal menu bar. We'll learn about <nav> in Semantic HTML.


Styling List Markers with CSS

The meaning of a list comes from HTML; the look of the markers comes from CSS. The main property is list-style-type:

ul.checklist { list-style-type: square; }
ol.steps     { list-style-type: upper-roman; }
nav ul       { list-style-type: none; padding-left: 0; }  /* menus: no bullets */

The old type attribute on <ul> (for example <ul type="square">) is obsolete — use CSS instead. On <ol>, the type attribute is still valid, as we saw above.

One subtlety: in Safari, a list styled with list-style-type: none may stop being announced as a list by the VoiceOver screen reader. If the “list-ness” matters to users, keep the list semantics explicit by adding role="list" to the <ul>.


Worked Example: A Study Plan Page

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>GATE Study Plan</title>
</head>
<body>
  <h1>GATE Study Plan</h1>

  <h2>Daily routine</h2>
  <ol>
    <li>Revise yesterday's notes</li>
    <li>Learn one new topic</li>
    <li>Solve 20 previous-year questions</li>
  </ol>

  <h2>Subjects</h2>
  <ul>
    <li>Core
      <ul>
        <li>Data Structures</li>
        <li>Algorithms
          <ul>
            <li>Sorting</li>
            <li>Graph algorithms</li>
          </ul>
        </li>
      </ul>
    </li>
    <li>Maths
      <ul>
        <li>Discrete Mathematics</li>
      </ul>
    </li>
  </ul>

  <h2>Top 3 resources</h2>
  <ol reversed>
    <li>Previous-year papers</li>
    <li>Standard textbooks</li>
    <li>Mock tests</li>
  </ol>

  <h2>Glossary</h2>
  <dl>
    <dt>PYQ</dt>
    <dd>Previous-year question.</dd>
    <dt>MSQ</dt>
    <dd>Multiple-select question: one or more options can be correct.</dd>
  </dl>
</body>
</html>

When opened in Chrome you'll see:

  • The daily routine numbered 1, 2, 3.
  • The subjects with three levels of markers: discs for “Core” and “Maths”, hollow circles for the subjects inside them, and small squares for “Sorting” and “Graph algorithms”.
  • The resources numbered 3, 2, 1 from top to bottom.
  • The glossary terms at the left edge with their descriptions indented below.

Choosing the Right List

ContentBest element
Steps of an algorithm or recipe<ol>
Features of a product<ul>
Ranking / leaderboard<ol> (maybe reversed)
Navigation menu<ul> inside <nav>
Glossary, FAQ, key–value details<dl>
Data with several columns (name, marks, grade)Not a list — use a table (see Tables)

You may also come across the <menu> element. In current HTML it means the same as <ul>, intended for a toolbar of commands; browsers render it like a <ul>. For ordinary lists, stick to <ul>.


Common Mistakes

  • Putting a nested list directly inside <ul>/<ol> instead of inside an <li>.
  • Typing numbers or bullets by hand (“1.”, “-”, “*”) inside paragraphs instead of using a list.
  • Using <br> to separate items: Item 1<br>Item 2<br> is not a list to the browser or a screen reader.
  • Forgetting to close <li> in nested lists, so the structure isn't what you think. (The end tag is technically optional, but writing it avoids mistakes.)
  • Using <ul type="circle"> — obsolete; use CSS.
  • Choosing <ol> just to get numbers when order doesn't matter, or <ul> for steps where order does.

Interview and Exam Questions

1. What are the three types of lists in HTML?

Unordered list (<ul>) for items where order doesn't matter, ordered list (<ol>) for items where order matters, and description list (<dl> with <dt> and <dd>) for term–description groups.

2. Write an ordered list that starts at C and uses uppercase letters.
<ol type="A" start="3">
  <li>Third item</li>
  <li>Fourth item</li>
</ol>

start is always an integer; with type="A", 3 is shown as “C”.

3. What is the correct way to nest a list?

Place the inner <ul> or <ol> inside an <li> of the outer list, before that <li>'s closing tag. <ul> and <ol> may only have <li> (and script-supporting elements) as children.

4. What does the reversed attribute do?

It is a boolean attribute on <ol> that makes the numbering count down (e.g. 3, 2, 1). The order of the items themselves is unchanged.

5. How do you remove bullets from a list?

With CSS: list-style-type: none; (often with padding-left: 0;). The type attribute on <ul> is obsolete.

6. Why are navigation menus usually written as lists?

A menu is a group of related links. Marking it up as a <ul> inside <nav> gives it structure that screen readers announce (number of items, position) and that is easy to style with CSS.

7. Can a <dd> appear without a <dt>?

No. In a valid <dl>, each group starts with one or more <dt> elements followed by one or more <dd> elements.


Summary

  • <ul> = order doesn't matter; <ol> = order matters; <dl> = terms and descriptions.
  • Every item in <ul>/<ol> is an <li>; nothing else goes directly inside them.
  • <ol> supports type (1, a, A, i, I), start and reversed; <li> supports value.
  • Nest lists inside an <li>.
  • Navigation menus are lists of links inside <nav>.
  • Marker style is a CSS job (list-style-type); <ul type> is obsolete.

When your data has rows and columns, a list is no longer enough. That's where the next chapter, Tables, comes in.