# Unordered & Ordered Lists

> Two containers, one question: does order matter? Use `<ul>` for a bag of peers and `<ol>` when sequence is part of the meaning.

Subject: HTML | Lesson 8 of 42 | 5 min read
Interactive lesson: https://codewithanimation.in/html/lists
Previous: [Preformatted Text & Quotes](https://codewithanimation.in/html/preformatted-and-quotes.md) | Next: [Description & Nested Lists](https://codewithanimation.in/html/description-and-nested-lists.md)

## What they are

`<ul>` and `<ol>` are the two list containers. `<ul>` is unordered: a collection of items where the sequence carries no meaning, a shopping list, a feature set, a group of options. `<ol>` is ordered: a sequence where position matters, recipe steps, a countdown, a numbered tutorial.

Both hold `<li>` elements and nothing else as direct children. An `<li>` can contain any block or inline content, including another nested list, so hierarchy comes naturally.

## Why it matters

The choice between `<ul>` and `<ol>` is not cosmetic. Screen readers announce the kind of list before reading it. A numbered list primes the listener for a sequence; a bulleted list signals that the items are peers. Using `<ol>` for ingredients just because you like numbers tells the listener to expect a sequence where none exists.

Lists are also the right container for navigation menus, for any genuinely enumerable set, and for groups of related items that belong together conceptually. A list makes that structure visible to browsers, search engines, and assistive tech alike.

## How it works

Write your items inside `<li>` tags, wrap them in `<ul>` or `<ol>`, and the browser renders bullets or numbers automatically. To change the marker style, reach for CSS `list-style-type` rather than switching the list element.

`<ol>` has three optional attributes that control the counter. `start` sets where counting begins, which is useful when one logical list is split across multiple sections of a page. `reversed` counts down instead of up. `type` switches the marker character: `1` for decimal (the default), `a` for lowercase letters, `A` for uppercase, `i` for lowercase roman numerals, `I` for uppercase roman. These are content attributes, not style, because the number itself carries meaning in a legal outline or a multi-part tutorial.

## Common mistake: Using a list just for the visual indentation

A `<ul>` or `<ol>` without genuinely listed content is a semantic mismatch. If you want indentation, use CSS `padding-left`. If you are pairing terms with definitions, reach for `<dl>`. Reserve list elements for collections of parallel items that actually belong together as a list.

## Tip: One question picks the tag

Would swapping two items change the meaning? If yes, use `<ol>`. If no, use `<ul>`. A shopping list stays a `<ul>` even if you happen to write it alphabetically. Recipe steps are an `<ol>` because doing step 3 before step 1 produces something different. That single test covers most cases.

## Try it: Try it

Toggle between `<ul>` and `<ol>`. When ordered, explore the `start` number and the `reversed` attribute.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/lists

## Check yourself

### Question 1

You are marking up the steps to reset a password. Which element do you use?

- A. <ol>, because the steps must happen in order
- B. <ul>, because steps are just a list of things
- C. <li> on its own, without a wrapper

Correct answer: A

When sequence is part of the meaning, use `<ol>`. A password-reset flow has a required order, so `<ol>` communicates that to both humans and assistive technology.

- Why not B: Order matters here. Doing step 3 before step 1 would fail, so the list is ordered.
- Why not C: <li> must live inside a <ul>, <ol>, or <menu>. A bare <li> is invalid.

### Question 2

A tutorial splits steps 4 to 6 into a new section. How do you make that <ol> start at 4?

- A. Add start="4" to the <ol>
- B. Add value="4" to the first <li>
- C. Use CSS counter-reset in a stylesheet

Correct answer: A

`start` on `<ol>` tells the browser which number to count from. It is a content attribute because the number itself has meaning when a single logical list spans multiple sections.

- Why not B: The value attribute on <li> overrides that one item's counter, but start on <ol> is the right way to set the whole list's beginning.
- Why not C: CSS counter properties style the display of counters; they do not set the semantic starting number. Use start on the <ol>.

### Question 3

What is the only valid direct child of <ul> and <ol>?

- A. <li>
- B. Any inline element such as <span> or <a>
- C. <div> for grouping related items

Correct answer: A

The content model for `<ul>` and `<ol>` allows only `<li>` as a direct child. Everything else goes inside the `<li>`.

- Why not B: Inline elements belong inside <li>, not directly inside <ul> or <ol>.
- Why not C: A <div> directly inside <ul> or <ol> is invalid. Wrap any grouping content inside an <li> first.

## Remember this

- `<ul>` for items where order does not matter; `<ol>` for items where sequence is part of the meaning.
- `<ol>` accepts `start` (first number), `reversed` (count down), and `type` (1/a/A/i/I marker style).
- Both `<ul>` and `<ol>` take only `<li>` as direct children; anything else belongs inside an `<li>`.
- Change bullet or number appearance with CSS `list-style-type`, not by switching the list element.
