Sub topic 15
Ordered lists
This chapter assumes you can write a basic HTML document and that you know how ul and li work from the previous chapter. Ordered lists use the same structure with one difference: the order of the items carries meaning.
When to use ol
Use ol when changing the order of the items would change the meaning. Steps in a recipe, rankings, and instructions are ordered. A shopping list is not, so use ul for it.
The browser generates the numbers. You do not type them.
<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 two existing ones, every later number updates automatically. This is the main reason to prefer ol over typing "1.", "2.", "3." into paragraphs. Typed numbers are plain text, so screen readers and other tools cannot tell the content is a list.
Controlling the numbers
Two attributes on ol change how counting works.
startsets the number of the first item.reversedis a boolean attribute that makes the list count down.
You can combine them.
<ol start="3" reversed>
<li>Bronze</li>
<li>Silver</li>
<li>Gold</li>
</ol>This list is numbered 3, 2, 1. Without start, a reversed list begins at the number of items it contains, which is also 3 here. The start value is still useful when you want a different number, for example to continue a list that was interrupted by a paragraph or a code sample.
Two related features exist. The type attribute on ol selects the marker style: 1, a, A, i, or I. The value attribute on a single li resets the count from that item. For style-only changes, CSS list-style-type is usually the better choice. Use start, reversed, and value when the number is part of the content.
Common errors
- Putting other elements directly inside
ol. Onlylielements (and script-supporting elements) are valid children. Wrap a paragraph in anliinstead of placing it between items. - Using
olonly to get numbers. If the order has no meaning, useuland style it. - Typing numbers into the items. You then get "1. 1. Preheat the oven." because the browser adds its own number.
- Expecting
startto accept text. It takes an integer. For letters, combinetype="a"with a numericstart:start="3"gives "c".
Checking your work
Open the page in a browser and read the numbers. Then add an item in the middle of the source. If the numbers fix themselves, the list is doing its job.
Exercise
You write <ol start="5" reversed> with four li items. What numbers does the browser show, from the first item to the last?
Show answer
5, 4, 3, 2. The start attribute sets the first number to 5, and reversed makes each following item count down by one.
Exercise
A colleague wrote a recipe with paragraphs that begin "1.", "2.", and "3.". Name two problems with this and the markup that fixes them.
Show answer
The numbers do not update when steps are added or moved, and assistive technology does not recognize the content as a list. Use an ol with one li per step.