6 / 10 · Intro
Lists
A shopping list and a recipe are both lists, but only one of them breaks if you change the order. HTML has a different element for each.
Jump to the 11 practice questions1Explain
Unordered and ordered lists
If the order of the items does not matter, use an unordered list, <ul>. Browsers mark each item with a bullet. If the order does matter, as in the steps of a recipe or a ranking, use an ordered list, <ol>, and the browser numbers the items for you. In both cases every visible item is an <li>, a list item. HTML also allows <script> and <template> directly inside <ul> or <ol>, but they do not create list items.
<h2>You will need</h2>
<ul>
<li>500 g flour</li>
<li>10 g salt</li>
<li>350 ml warm water</li>
</ul>
<h2>Method</h2>
<ol>
<li>Mix everything in a bowl.</li>
<li>Leave it covered for 12 hours.</li>
<li>Shape and bake at 230 degrees for 40 minutes.</li>
</ol>2Explore
Options for ol
<ol> has a few attributes. start="4" begins the count at 4, useful when a list continues after a paragraph. reversed counts down, for a top ten. type switches the marker to letters or Roman numerals: type="a", type="A", type="i" or type="I". A single item can also jump to a number with value, as in <li value="10">. How the bullets and numbers look (their shape, colour, or whether they show at all) is controlled by CSS.
The steps stay the same; only the numbering changes.
Tap a choice: the code and the preview change together.
<ol>
<li>Mix</li>
<li>Knead</li>
<li>Bake</li>
</ol>Nothing is drawn: there is no visible content.
3Problem
start="5" with three items
An ol has start="5" and 3 items. What number does the last item show?
Hint
Count 5, 6, …
Solution
Answer: 7
start sets the first number, so three items show 5, 6 and 7.
4Problem
A reversed list
A reversed ol has 10 items. What does the first item show?
Hint
reversed counts down to 1.
Solution
Answer: 10
It counts down from the number of items: 10, 9, ... 1.
5Problem
Roman numerals from three
<ol type="i" start="3">
<li>Shape</li>
<li>Proof</li>
</ol>What marker appears before Shape?
Hint
type sets the kind of numbering, start sets where it begins.
Solution
Answer: iii
type="i" uses small Roman numerals and start="3" begins at three, so the markers are iii and iv. start is always written as an ordinary number.
6Explain
Lists inside lists
A list item can contain a whole list. Put the inner <ul> or <ol> inside the <li> it belongs to, after that item's text and before its closing tag. A common mistake is to place the inner list between two <li> elements, directly inside the outer list, which is not allowed. Nested lists are how sites build menus with submenus and how outlines show sub-points.
<ul>
<li>Bread
<ul>
<li>Sourdough</li>
<li>Rye</li>
</ul>
</li>
<li>Cakes</li>
</ul>7Problem
Where a nested list goes
Where does a nested list go?
Hint
It belongs to one item.
Solution
Answer: Inside the li it belongs to
A list placed directly inside ul, between items, is invalid.
8Problem
Items in the outer list
<ul>
<li>Drinks
<ul>
<li>Tea</li>
<li>Coffee</li>
</ul>
</li>
<li>Cakes</li>
<li>Pastries</li>
</ul>How many items does the outer list have?
Hint
The inner list belongs to one item; its items are not counted in the outer list.
Solution
Answer: 3
Drinks, Cakes and Pastries: 3. Tea and Coffee are items of the inner list inside Drinks. A screen reader announces the outer list as a list of 3 items.
9Explain
Description lists
The third kind of list pairs terms with descriptions, like a glossary or a set of questions and answers. <dl> is the description list, <dt> holds a term, and <dd> holds its description. One term can have several descriptions, and several terms can share one. Browsers indent the descriptions by default.
<dl>
<dt>Crumb</dt>
<dd>The soft inside of a loaf.</dd>
<dt>Crust</dt>
<dd>The hard outside.</dd>
</dl>10Problem
The word being explained
In a description list, which element holds the word that is explained?
Hint
t for term, d for description.
Solution
Answer: dt
dt holds the term and dd its description, both inside dl. li belongs to ul and ol.
11Explain
Which list to use
A quick way to choose between the two: imagine shuffling the items. If shuffling changes the meaning, as it would for the steps of a recipe or a top ten, the list is ordered and needs <ol>. If nothing changes, as with a packing list, use <ul>. Keep each item short and start items in the same grammatical way, so the list is easy to scan.
| Element | Use it when | Example |
|---|---|---|
| ul | The order does not matter | A shopping list |
| ol | The order matters | The steps of a recipe |
| dl | Each item is a term with a description | A glossary |
12Problem
Typed numbers
Why use a real list instead of typing 1. 2. 3.?
Hint
Think of a screen reader announcing the list.
Solution
Answer: Screen readers announce the list and its length, and numbers update themselves
Structure is information that software can use.
13Practice
Two kinds of list
Turn the packing list into a <ul> with 3 items, and the steps into an <ol> with 4 items (no typed numbers).
- ul with 3 items
- ol with 4 items
- steps in order
<h2>Packing list</h2>
<p>Water bottle, rain jacket, map</p>
<h2>Before you leave</h2>
<p>1. Check the weather. 2. Charge your phone. 3. Tell someone your route. 4. Lock the door.</p>Hint
Packing: order does not matter. Steps: order matters, and the browser numbers them.
A solution
<h2>Packing list</h2>
<ul>
<li>Water bottle</li>
<li>Rain jacket</li>
<li>Map</li>
</ul>
<h2>Before you leave</h2>
<ol>
<li>Check the weather.</li>
<li>Charge your phone.</li>
<li>Tell someone your route.</li>
<li>Lock the door.</li>
</ol>14Practice
A nested list and a glossary
Nest a list with Sourdough and Rye inside the Bread item. Then turn the two definitions into a <dl> with <dt> and <dd>.
- nested list in an item
- Sourdough and Rye
- two terms (dt)
- two descriptions (dd)
<ul>
<li>Bread</li>
<li>Cakes</li>
</ul>
<p>Crumb: the soft inside of a loaf.</p>
<p>Crust: the hard outside.</p>Hint
The inner ul goes after the word Bread and before </li>.
A solution
<ul>
<li>Bread
<ul>
<li>Sourdough</li>
<li>Rye</li>
</ul>
</li>
<li>Cakes</li>
</ul>
<dl>
<dt>Crumb</dt>
<dd>The soft inside of a loaf.</dd>
<dt>Crust</dt>
<dd>The hard outside.</dd>
</dl>15Explain
Common mistakes
<ul>
<li>Bread</li>
<ul><li>Rye</li></ul>
</ul><ul>
<li>Bread
<ul><li>Rye</li></ul>
</li>
</ul>A list directly inside ul is not allowed. The inner list belongs inside the li of Bread.
16
Summary
- ul when order does not matter, ol when it does; every item is an li.
- Nested lists go inside an li.
- dl, dt and dd pair terms with descriptions.