Sub topic 14
Unordered lists
This chapter assumes you can write a basic HTML document (html, head, body), open it in a browser, and use elements and attributes. It covers the ul and li elements, the rules for what can go inside them, and the mistakes that most often break list markup.
What an unordered list is
An unordered list is a group of items where the order does not change the meaning. A shopping list is a good example. Whether "Bread" comes before "Cheese" makes no difference. The ul element (unordered list) is the container. Each item is an li element (list item).
<ul>
<li>Apples</li>
<li>Bread</li>
<li>Cheese</li>
</ul>Browsers draw a bullet before each item and indent the list. Those bullets and that indentation come from the browser's default stylesheet. They are not part of the meaning of the list. You can change both with CSS without changing the HTML.
The working model
A list is a parent with children, and the browser, search engines, and assistive technology all read that structure.
ulis the parent. Its only valid direct children arelielements. Script-supporting elements such asscriptandtemplateare also allowed.liis a child. Its content can be text, links, images, or whole other lists.- The list's meaning comes from the elements, not from how they look.
Screen readers use this structure. When a user reaches a list, many screen readers announce something like "list, three items", and then "1 of 3" as the user moves through the items. A visitor who cannot see the page learns how long the list is and where they are in it. Lines separated by br elements, or paragraphs that only look like a list, give them none of that.
The diagram below shows the parent and child relationship.
What can go inside an li
An li accepts "flow content". That means almost anything you can put in the body of a page: text, p, a, img, strong, headings, even another list. This makes ul useful for more than plain text.
<ul>
<li><a href="/articles/">Articles</a></li>
<li><strong>Note:</strong> prices include tax.</li>
<li>
<p>A longer item with its own paragraph.</p>
<p>And a second paragraph in the same item.</p>
</li>
</ul>Put the nested content inside the li, not beside it. The next section covers why.
Common errors
Most invalid lists fall into a few patterns. Browsers usually render them anyway, which hides the mistake. Run your page through the W3C validator to catch them.
Placing something other than li directly inside ul
This is invalid:
<ul>
<p>Fruit</p>
<li>Apples</li>
</ul>The p is not a permitted child of ul. If you need a label for the list, put it before the ul, or use a heading.
<h2>Fruit</h2>
<ul>
<li>Apples</li>
</ul>Putting a nested list beside the li instead of inside it
A nested list belongs to the item it expands on. This is the correct form:
<ul>
<li>Fruit
<ul>
<li>Apples</li>
<li>Pears</li>
</ul>
</li>
<li>Vegetables</li>
</ul>A ul placed directly inside another ul, between two li elements, is invalid. It also reads badly in assistive technology, because the sub-list is not attached to any parent item. Nested lists get their own chapter, so this one only covers the rule.
Using a list for layout or indentation
Developers sometimes use ul just to indent text, or br to fake a list. Use a list only when the content really is a group of related items. For indentation, use CSS.
Using a list when the order matters
If the steps must happen in sequence, use ol instead, which the next chapter covers. A recipe written with ul loses the sequence for anyone who cannot see the layout.
Removing the bullets and losing the list
Setting list-style: none in CSS is common for menus. Some browsers, notably Safari with VoiceOver, stop announcing a list when its bullets are removed. If the content is a real list, you can restore the role explicitly:
<ul role="list" style="list-style: none; padding: 0;">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>The role="list" attribute is normally redundant on a ul. It is worth adding only in this situation, where styling has removed the list semantics in some browsers.
Styling the bullets with CSS
You do not need to change the HTML to change the marker. The list-style-type property controls it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>List styles</title>
<style>
.squares { list-style-type: square; }
.plain { list-style: none; padding-left: 0; }
</style>
</head>
<body>
<ul class="squares">
<li>Square marker</li>
<li>Another item</li>
</ul>
<ul class="plain" role="list">
<li>No marker</li>
<li>Another item</li>
</ul>
</body>
</html>Save this as a file and open it in a browser. The first list shows square markers. The second shows none.
Choosing between list types
| Content | Element | Reason |
|---|---|---|
| Ingredients, features, tags | `ul` | Order does not matter |
| Steps, rankings | `ol` | Order carries meaning |
| Terms and definitions | `dl` | Each item is a name and value pair |
| Site menu | `ul` inside `nav` | A group of links with no required order |
Edge cases
- Empty lists. A
ulwith nolichildren is valid in some parsers but pointless. Screen readers may announce "list, 0 items". Remove empty lists. - A one-item list. This is legal, but ask whether it needs to be a list at all. A single item is usually a paragraph.
- The
valueattribute. It applies toliinside anol, not aul. Puttingvalueon anliin aulhas no visible effect. - Whitespace. Spaces and line breaks between
lielements do not affect the layout of a normal list. They only matter if you make the itemsinline-blockwith CSS. - Closing tags.
</li>can be omitted in some cases by the HTML specification. Always write it. Explicit closing tags avoid surprises when you nest content.
Exercise
This markup has two problems. What are they, and how do you fix them? <ul> <li>Fruit</li> <ul> <li>Apples</li> </ul> <p>Vegetables</p> </ul>
Show answer
First, the nested ul sits directly inside the outer ul instead of inside the "Fruit" li. Move it inside that li, after the text "Fruit". Second, the p element is not a valid direct child of ul. Wrap "Vegetables" in an li, so it becomes <li>Vegetables</li>. The corrected list is a ul with two li children: the first holds "Fruit" and the nested ul, and the second holds "Vegetables".
Exercise
You have a site menu where the bullets are hidden with list-style: none. Which attribute can you add to the ul so that VoiceOver in Safari still announces it as a list?
Show answer
Add role="list" to the ul, as in <ul role="list">. It restores the list semantics that some browsers drop when the markers are removed.