Lesson 5 of 29 · HTML Basics
HTML Lists: ul, ol, li and Nested Lists
Lists are everywhere on the web: navigation menus, recipes, search results, the steps of a tutorial. HTML has two main kinds — bulleted and numbered — and you can put a list inside a list to show a hierarchy.
Unordered lists
Use an unordered list, <ul>, when the order of the items doesn't matter. Each item is an <li> (list item), and the browser draws a bullet in front of it:
<ul>
<li>Sword</li>
<li>Shield</li>
<li>Potions</li>
</ul>
Ordered lists
Use an ordered list, <ol>, when the order does matter: steps, rankings, directions. The browser numbers the items for you:
<ol>
<li>Open the chest</li>
<li>Take the key</li>
<li>Unlock the door</li>
</ol>
Because the numbers are generated, you can add or remove a step and the whole list renumbers itself. Need to start somewhere else? <ol start="5"> begins counting at 5.
Nested lists
A list item can contain a whole new list. Put the inner <ul> or <ol> inside the <li>, after the item's text and before its closing </li>:
<ul>
<li>Weapons
<ul>
<li>Sword</li>
<li>Bow</li>
</ul>
</li>
<li>Armour</li>
</ul>
The browser indents the inner list and gives it a different bullet style, so the hierarchy is visible at a glance. You can nest as deep as you like, and mix the two kinds: numbered steps with bulleted details, for example.
Why use real lists?
You could fake a list with paragraphs and dashes, but a real HTML list carries meaning. A screen reader announces "list, 3 items" so the listener knows what's coming — and can skip the whole list in one go. That's also why navigation menus are built as lists of links, as you'll see later in the course.
Common mistakes
- Content directly inside
<ul>or<ol>. Their only children are<li>elements; text and other elements go inside an<li>. - A nested list next to its
<li>instead of inside it.</li><ul>…</ul>breaks the structure — the inner list belongs before the</li>. - Typing the numbers yourself.
<li>1. Open the chest</li>inside an<ol>shows up as "1. 1. Open the chest". - Using a list just to indent text. Indentation is a job for CSS.
Assignment
Fill in the adventurer's notes:
- Under
Inventory, an unordered list with the itemsSword,ShieldandPotions. - Inside the
Potionsitem, a nested unordered list withHealing potionandMana potion. - Under
Quest steps, an ordered list:Find the map,Cross the river,Defeat the troll.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .