Sub topic 9
Ordered, Unordered and Description Lists
This chapter assumes you can write a basic HTML document and know what elements, attributes and nesting are. Lists group related items, and they tell browsers and assistive technology that the items belong together. HTML has three kinds: unordered, ordered and description lists.
Unordered lists
Use <ul> when the order of the items does not matter. Each item goes in an <li> (list item) element.
<ul>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>Browsers show bullets by default. You change the bullet style with CSS, not with HTML attributes.
Ordered lists
Use <ol> when the sequence matters, such as steps or rankings. Browsers number the items automatically.
<ol>
<li>Write the markup</li>
<li>Open it in a browser</li>
</ol>Three attributes adjust the numbering:
| Attribute | Applies to | Effect |
|---|---|---|
| `start` | `ol` | Sets the first number, for example `start="5"` |
| `reversed` | `ol` | Counts down instead of up |
| `type` | `ol` | Chooses `1`, `a`, `A`, `i` or `I` markers |
<ol start="3" type="a">
<li>Third item, shown as "c"</li>
<li>Fourth item, shown as "d"</li>
</ol>The value attribute on a single <li> sets that item's number and the items after it continue from there.
Description lists
Use <dl> for name and value pairs, such as glossary terms or metadata. A <dt> holds the term and a <dd> holds its description.
<dl>
<dt>Element</dt>
<dd>A start tag, content and an end tag.</dd>
<dt>Attribute</dt>
<dd>A name and value inside a start tag.</dd>
<dd>Example: <code>href="/"</code></dd>
</dl>One term can have several descriptions, and several terms can share one description.
Nesting lists
A nested list goes inside an <li>, not directly inside the parent <ul> or <ol>.
<ul>
<li>Fruit
<ul>
<li>Apple</li>
<li>Pear</li>
</ul>
</li>
<li>Vegetables</li>
</ul>Common errors
- Putting text or other elements directly inside
<ul>or<ol>. Only<li>is allowed as a direct child (script-supporting elements aside). - Placing a nested
<ul>as a sibling of<li>instead of inside it. - Using
<br>or-characters to fake a list. Screen readers then cannot announce "list, 3 items". - Using
<ul>just to indent text. Use CSS for layout. - Using a list for an ordered process but choosing
<ul>. If the sequence matters, choose<ol>.
Choosing the right list
- Does the sequence matter? Use
<ol>. - Is each entry a term with a description? Use
<dl>. - Otherwise, use
<ul>.
Exercise
You are marking up a recipe with ingredients and numbered cooking steps. Which list elements do you use for each, and where does a note under step 2 ("Use a deep pan") go if it is a list of its own?
Show answer
Use <ul> for the ingredients because their order does not matter, and <ol> for the steps because order matters. A list under step 2 goes inside the second <li> of the <ol>, not as a direct child of the <ol>.
Exercise
What is wrong with this markup, and how do you fix it? <ul><li>One</li><p>Two</p></ul>
Show answer
A <p> is not allowed as a direct child of <ul>. Wrap the text in an item: <ul><li>One</li><li>Two</li></ul>.