Sub topic 14
Unordered Lists
This chapter assumes you know how to write elements and attributes and can open an HTML file in a browser. It also assumes you have a basic HTML document with html, head, and body.
The working model
An unordered list is a group of items whose order does not change their meaning. You build it from two elements:
ulis the list container.liis one list item.
Browsers draw a bullet before each item by default. The bullet is a visual style, and CSS can change or remove it. The ul element still tells browsers and assistive technology that the content is a list. A screen reader can announce "list, 3 items" before reading the entries.
<ul>
<li>Apples</li>
<li>Bread</li>
<li>Cheese</li>
</ul>Deciding whether a list is the right element
Ask whether the items would still make sense if you shuffled them. If they would, use ul. If the sequence matters, as in recipe steps, use an ordered list instead (covered in the next chapter).
What an li can contain
An li accepts text, links, images, and other flow content, so you can put richer markup inside an item.
<ul>
<li><a href="/docs/intro.html">Introduction</a></li>
<li><strong>Note:</strong> drafts are not published.</li>
<li>
<img src="images/fox-icon.png" alt="" width="24" height="24">
Wildlife photos
</li>
</ul>Nesting a list inside an item is also valid. The Nesting lists chapter covers it.
Common errors
The rules are strict about what may sit directly inside a ul:
- Text or other elements as direct children. Only
lielements (and script-supporting elements) belong directly insideul. Wrap loose text in anli. - An
lioutside any list. Anlineeds aulorolparent. Without one, the markup is invalid and its meaning is unclear. - Using a list for layout. Do not use
ulonly to indent text or to get bullets. Use CSS for appearance. - Faking a list. Typing
•or-at the start of paragraphs gives no list structure. Screen reader users get no item count and no list navigation. - Removing bullets without keeping the semantics. Setting
list-style: nonein CSS is fine for menus. Keep theulandlielements so the structure remains.
<!-- Invalid: text sits directly inside ul -->
<ul>
Apples
<li>Bread</li>
</ul>
<!-- Valid -->
<ul>
<li>Apples</li>
<li>Bread</li>
</ul>Exercise
A page has three paragraphs that each begin with the character "•" followed by a feature name. What should you change, and why?
Show answer
Replace the paragraphs with one ul containing three li elements, one per feature, and delete the typed "•" characters. The browser then supplies the bullets, and assistive technology can announce the group as a list with three items.