Sub topic 15
Ordered lists
This chapter assumes you can write ul and li elements, as covered in the previous chapter, and that you know how elements and attributes work. Here you will learn when to use an ordered list and how to control its numbering. You will also learn how to avoid the mistakes that break it.
When order carries meaning
An ordered list is a list where the sequence of items matters. If you could shuffle the items without changing the meaning, use ul. If shuffling would change the meaning, use ol.
Typical cases are:
- Steps in a procedure, such as a recipe or an install guide.
- Rankings, such as a top-ten list or race results.
- Items that other text refers to by number, such as "see step 3".
The browser numbers the items for you. You do not type the numbers into the HTML.
<ol>
<li>Preheat the oven.</li>
<li>Mix the ingredients.</li>
<li>Bake for 30 minutes.</li>
</ol>If you insert a new step between steps 1 and 2, the later items renumber themselves. This is the main benefit of ol over typing "1.", "2." by hand inside paragraphs. It also tells assistive technology that the content is a list. A screen reader can announce "list, 3 items" and read the position of each item.
Content rules for ol
The only elements allowed as direct children of ol are li, plus script-supporting elements such as script and template. Text, p, and another ol placed directly inside an ol are all invalid. Browsers will usually display them, but validators will flag them. Assistive technology may also announce the structure incorrectly.
An li can hold almost any flow content: paragraphs, images, links, code blocks, or another list.
Controlling the numbers
Three attributes on ol change how the numbers are produced. One attribute on li overrides a single number.
| Attribute | Element | Effect |
|---|---|---|
| `start` | `ol` | Sets the number of the first item. It must be an integer, and negative values and 0 are allowed. |
| `reversed` | `ol` | A boolean attribute. The list counts down instead of up. |
| `type` | `ol` | Chooses the marker style: `1`, `a`, `A`, `i`, or `I`. |
| `value` | `li` | Sets the number of this item. The following items continue from it. |
start
Use start when a list continues after an interruption, for example when a figure or a long explanation splits a procedure into two lists.
<ol>
<li>Open the settings page.</li>
<li>Select the Network tab.</li>
</ol>
<p>The next steps depend on your connection type.</p>
<ol start="3">
<li>Enter the server address.</li>
<li>Save the changes.</li>
</ol>reversed
The reversed attribute makes the numbers descend. If you do not give a start value, the list begins at the number of items. Here is a complete page that shows a countdown ending at 1.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Podium</title>
</head>
<body>
<h1>Podium</h1>
<ol start="3" reversed>
<li>Bronze</li>
<li>Silver</li>
<li>Gold</li>
</ol>
</body>
</html>The items display as 3, 2, 1. The start="3" here is redundant, because a three-item reversed list already starts at 3. It becomes necessary when you want a countdown that begins at a different number than the item count.
type
The type attribute picks the marker style.
<ol type="A">
<li>Introduction</li>
<li>Method</li>
<li>Results</li>
</ol>
<ol type="i">
<li>First clause</li>
<li>Second clause</li>
</ol>The values a and A give letters, and i and I give Roman numerals. The attribute is valid for ol. It is meant for cases where the marker is part of the content, such as a legal document that cites "clause (iii)". If the style is only decoration, use the CSS property list-style-type instead, so presentation stays out of the markup.
<style>
ol.steps { list-style-type: decimal-leading-zero; }
</style>
<ol class="steps">
<li>Download the file.</li>
<li>Run the installer.</li>
</ol>CSS also offers styles that type cannot express, such as decimal-leading-zero (01, 02, 03) and lower-greek.
li value
The value attribute sets the number of one item. All later items continue counting from that value.
<ol>
<li>Registration</li>
<li value="10">Lunch break</li>
<li>Afternoon session</li>
</ol>The markers here are 1, 10, 11. Use this rarely. If many items need explicit values, you probably want separate lists with start.
The following diagram shows how the browser decides the number for each item.
Nesting ordered lists
A nested list belongs inside an li, not directly inside the parent ol. Each nested ol starts its own count.
<ol>
<li>Prepare the workspace
<ol type="a">
<li>Clear the desk.</li>
<li>Open the editor.</li>
</ol>
</li>
<li>Create the file</li>
</ol>The outer items show 1 and 2, and the inner items show a and b. Using a different type at each level makes references such as "step 1.a" easy to follow.
Common errors
- Typing numbers into items. Writing
<li>1. Preheat</li>produces "1. 1. Preheat". Let the browser number the list. - Placing a nested list outside an
li. A list placed directly inside the parentolis invalid. Put it inside the precedingli. - Using
olfor layout. If the order has no meaning, useul. Choose the list type by meaning, not by the look of the markers. - Expecting
reversedto reverse the display order. It changes only the numbers. The first item in the source is still shown first. To show Gold first, write Gold first in the source. - Quoting
startas text. Writingstart="three"is invalid. The value must be an integer. - Removing markers without checking accessibility. Setting
list-style: noneon a list can cause some screen reader and browser pairings to stop announcing it as a list. If you remove the markers, test with a screen reader. Or addrole="list"to the element.
Exercises
Exercise
A tutorial has steps 1 to 4, then an image, then steps 5 and 6. Write the markup for the second list so that it continues numbering from 5.
Show answer
Use <ol start="5"> with two li children, for example <ol start="5"><li>Save the file.</li><li>Reload the page.</li></ol>.
Exercise
What does the list show for <ol reversed><li>A</li><li>B</li><li>C</li><li value="10">D</li><li>E</li></ol>?
Show answer
A list with five items that is reversed and has no start begins at 5. The markers are 5, 4, 3 for A, B, C. D has value="10", so it shows 10. E then continues downward from 10 and shows 9.
Exercise
This markup is invalid. Name the problem and fix it. <ol><li>Setup</li><ol><li>Install</li></ol><li>Run</li></ol>
Show answer
The nested ol is a direct child of the outer ol. Move it inside the first item: <ol><li>Setup<ol><li>Install</li></ol></li><li>Run</li></ol>.