So far you have written all your content in paragraphs. But a lot of information isn't running text; it's a series of items: the specialities on a menu, the steps for making a coffee, the definitions in a glossary. For this, HTML offers three types of list, each with a different meaning: unordered (ul), when the order doesn't matter; ordered (ol), when it does; and description lists (dl), for pairs of terms and definitions. In this lesson you will learn to build them, nest them, control their numbering and choose the right one for each case. With them you will finally create the content of menu.html.
Contents
- Why use lists
- Unordered lists:
ulandli - Ordered lists:
ol - Attributes of
ol:type,start,reversedandvalue - Nested lists
- Description lists:
dl,dtanddd - Which list to use in each case
- Lists as the basis of navigation menus
- Project: The Blue Cup's menu
Why use lists
You could fake a list with paragraphs and dashes:
<!-- Incorrect: it looks like a list, but it isn't one -->
<p>- Espresso</p>
<p>- Cortado</p>
<p>- Cappuccino</p>Visually it works, but the browser only sees three unrelated paragraphs. With a real list:
- A screen reader announces "list, 3 items" and lets the user jump from one item to the next or leave the list. The user knows in advance how much content lies ahead.
- The browser automatically generates the bullets or numbers, and adjusts them by itself if you add or remove items.
- With CSS you will be able to change the style of all lists at once, or turn them into horizontal menus.
General rule: if you would use bullets, dashes or numbers when writing by hand, in HTML you need a list.
Unordered lists: ul and li
An unordered list (ul, unordered list) groups items whose order doesn't change the meaning:
ulis the container for the list.- Each
li(list item) is an item in the list. - By default, the browser displays a bullet (a black disc) in front of each
liand indents the list from the left.
If you swap "Cortado" and "Cappuccino", the information stays the same: that's why it's unordered.
What can go inside
- The direct children of
ulcan only belielements. No paragraphs, no headings, no loose text. - Inside each
li, however, you can put almost anything: text, inline elements such asstrong, paragraphs and even other lists.
<!-- Incorrect: a p as a direct child of ul -->
<ul>
<p>House coffees:</p>
<li>Espresso</li>
</ul>
<!-- Correct: the introductory text goes outside the list -->
<p>House coffees:</p>
<ul>
<li>Espresso</li>
<li>Cortado <strong>(our most popular)</strong></li>
</ul>Ordered lists: ol
An ordered list (ol, ordered list) groups items whose order does matter: the steps in a process, a ranking, instructions.
<ol>
<li>Grind the coffee.</li>
<li>Heat the water.</li>
<li>Pour the water over the coffee.</li>
</ol>The browser numbers the items automatically: 1, 2, 3. If you change the order of the items, you change the meaning (you can't pour the water before heating it). Notice that you don't type the numbers: if you add a step in the middle, the numbering updates by itself.
Attributes of ol: type, start, reversed and value
Four attributes let you control the numbering:
| Attribute | Goes on | What it does | Example |
|---|---|---|---|
type |
ol |
Type of marker: 1, a, A, i, I |
<ol type="a"> → a, b, c |
start |
ol |
Number to start from (always an integer) | <ol start="4"> → 4, 5, 6 |
reversed |
ol |
Descending numbering (a Boolean attribute) | <ol reversed> → 3, 2, 1 |
value |
li |
Sets the number of that item; the following items continue from it | <li value="10"> |
<!-- Sub-items with letters -->
<ol type="a">
<li>With whole milk</li>
<li>With plant-based milk</li>
</ol>
<!-- Continuing an interrupted list: starts at 4 -->
<ol start="4">
<li>Serve in a warmed cup.</li>
<li>Serve with a glass of water.</li>
</ol>
<!-- Countdown: displays 3, 2, 1 -->
<ol reversed>
<li>Lemon sponge cake</li>
<li>Seasonal coffee</li>
<li>Butter croissant</li>
</ol>Important details:
startalways takes a number, even if thetypeuses letters:<ol type="A" start="3">starts at C.reversedis a Boolean attribute: just writing it is enough, as you saw in Tags, Elements and Attributes.- Use
typeonly when the type of numbering is part of the content (for example, if a text says "see point b"). If it's just a matter of appearance, it's done with CSS (thelist-style-typeproperty, in Module 8).
Nested lists
A list can contain another list to express sub-levels. The inner list goes inside an li, never directly inside ul or ol:
<ul>
<li>Black coffees
<ul>
<li>Espresso</li>
<li>Americano</li>
</ul>
</li>
<li>Milk coffees
<ul>
<li>Cortado</li>
<li>Cappuccino</li>
</ul>
</li>
</ul>Step by step:
- The "Black coffees"
liopens. - Inside it, after the text, a new
ulopens with its ownlielements. - The inner
ulcloses and then thelicontaining it closes.
graph TD
UL["ul"] --> L1["li: Black coffees"]
UL --> L2["li: Milk coffees"]
L1 --> U1["ul"]
U1 --> A["li: Espresso"]
U1 --> B["li: Americano"]
L2 --> U2["ul"]
U2 --> C["li: Cortado"]
U2 --> D["li: Cappuccino"]
The browser changes the type of bullet at each level (black disc, hollow circle, square) and increases the indentation.
You can mix types: an ordered list of steps whose steps contain an unordered list of options, for example. Don't overdo the depth: more than two or three levels usually means the content needs headings rather than lists.
Description lists: dl, dt and dd
A description list (dl, description list) groups name–value pairs: terms and definitions, questions and answers, pieces of data and their values.
<dl>
<dt>Espresso</dt>
<dd>A short, concentrated coffee made under pressure.</dd>
<dt>Cortado</dt>
<dd>An espresso with a little hot milk.</dd>
</dl>dlis the container.dt(description term) is the term.dd(description details) is its description. By default it appears on the next line, indented.
Valid combinations:
<dl>
<!-- One term with several descriptions -->
<dt>Cappuccino</dt>
<dd>An espresso with steamed milk and plenty of foam.</dd>
<dd>At The Blue Cup we serve it with a dusting of cocoa powder.</dd>
<!-- Several terms (synonyms) with one description -->
<dt>Americano</dt>
<dt>Long coffee</dt>
<dd>An espresso lengthened with hot water.</dd>
</dl>HTML also lets you wrap each dt + dd group in a div (you will see this element in Block and Inline Elements: div and span), which is useful for styling them; it isn't compulsory.
As well as glossaries, dl is useful for fact sheets (Origin → Colombia; Roast → Medium) or lists of frequently asked questions.
Which list to use in each case
| Question | If the answer is yes | Examples |
|---|---|---|
| Are the items of the same kind, and does the order not change the meaning? | ul |
Specialities, ingredients, features |
| Is the order part of the information? | ol |
Steps in a recipe, rankings, instructions |
| Is each item a name–description pair? | dl |
Glossary, technical specifications, FAQs |
| Is it continuous text with linked ideas? | None: paragraphs | An explanation or a story |
| Is it data with several columns to compare? | None: a table | Prices by size (Module 4) |
A trick for deciding between ul and ol: shuffle the items in your head. If the list is still correct, it's a ul; if it no longer is, it's an ol.
Lists as the basis of navigation menus
A navigation menu is nothing more than a list of links, usually unordered. That's why the vast majority of websites build their menus like this:
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="menu.html">Menu</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>Later on, CSS will turn that vertical bulleted list into a horizontal bar, and in header, nav and footer (Module 6) you will learn to wrap it in the semantic element that identifies it as the main navigation. For now, just remember the idea: a menu is a list.
Project: The Blue Cup's menu
Creating menu.html
menu.html has been empty since Module 1. Write this complete document in it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Menu | The Blue Cup</title>
</head>
<body>
<h1>Our menu</h1>
<p>Speciality coffee roasted in house and pastries from our bakery.</p>
<p><small>Prices include <abbr title="Value Added Tax">VAT</abbr>.</small></p>
<h2>House specialities</h2>
<ul>
<li>Autumn seasonal coffee: €2.50</li>
<li>Butter croissant: €2.40</li>
<li>Lemon sponge cake: €2.20</li>
<li>Sourdough bread (loaf): €3.00</li>
</ul>
<h2>Coffees</h2>
<ul>
<li>Black coffees
<ul>
<li>Espresso: €1.40</li>
<li><i lang="it">Ristretto</i>: €1.40</li>
<li>Americano: €1.60</li>
</ul>
</li>
<li>Milk coffees
<ul>
<li>Cortado: €1.50</li>
<li>White coffee: €1.80</li>
<li>Cappuccino: €2.20</li>
<li>Flat white: €2.40</li>
</ul>
</li>
</ul>
<h2>How we make our filter coffee</h2>
<ol>
<li>We weigh out 15 g of freshly roasted coffee.</li>
<li>We grind it to a medium grind just before brewing.</li>
<li>We heat the water to 92 °C and wet the paper filter.</li>
<li>We pour in a little water and wait 30 seconds for the coffee to "bloom".</li>
<li>We add the rest of the water, up to 250 ml, in slow circles.</li>
<li>We serve it as soon as the dripping stops, about three and a half minutes later.</li>
</ol>
<h2>Coffee glossary</h2>
<dl>
<dt>Espresso</dt>
<dd>A short, concentrated coffee made by forcing hot water under pressure through very finely ground coffee.</dd>
<dt><i lang="it">Ristretto</i></dt>
<dd>An even shorter espresso, made with less water: more intense and sweeter.</dd>
<dt>Americano</dt>
<dt>Long coffee</dt>
<dd>An espresso lengthened with hot water.</dd>
<dt>Cortado</dt>
<dd>An espresso "cut" with a little hot milk.</dd>
<dt>Cappuccino</dt>
<dd>An espresso with steamed milk and a generous layer of foam.</dd>
<dt>Flat white</dt>
<dd>A double espresso with steamed milk and very little foam, with a silky texture.</dd>
</dl>
</body>
</html>Look at the decisions:
- Specialities in a
ul: the order doesn't matter. Each price goes with its product in the sameli. (When the menu needs comparable columns, such as prices by size, you will use a table in Module 4.) - Coffees with a nested list: two categories and, within each one, its products. Notice that each inner
ulis inside theliof its category. - Brewing in an
ol: the steps have to be followed in order. - Glossary in a
dl: pairs of terms and definitions; "Americano" and "Long coffee" are synonyms that share a description. - Terms in another language (here, the Italian ristretto) are marked with
iandlang, as you learnt in Text Formatting Tags.
The prices add up with the offer on index.html: a white coffee (€1.80) plus a croissant (€2.40) come to the €4.20 shown struck through.
A countdown list in news.html
At the end of news.html, inside body, add a new news item with a descending ordered list:
<h2>Most ordered in October</h2>
<p>These were your three favourites this month, from fewest to most votes:</p>
<ol reversed>
<li>Lemon sponge cake</li>
<li>Autumn seasonal coffee</li>
<li>Butter croissant</li>
</ol>It will display 3, 2, 1: the croissant is number 1 and appears last, as in a countdown.
Turning the temporary links in index.html into a list
The five paragraphs with links at the end of index.html are really a list of links: a menu. Replace them with:
<!-- Temporary links to the other pages (to be improved in Modules 3 and 6) -->
<ul>
<li><a href="menu.html">Menu</a></li>
<li><a href="bookings.html">Bookings</a></li>
<li><a href="gallery.html">Gallery</a></li>
<li><a href="news.html">News</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>Save the three files and move between index.html and menu.html with Live Server. In DevTools, notice how the tree for menu.html reflects the nesting: ul > li > ul > li.
Common Mistakes and Tips
- Typing the numbers by hand inside an
ol("1. Grind…"): they will appear twice. Let the browser do the numbering. - Putting anything other than
lias a direct child ofulorol: headings or introductory text go outside the list. - Nesting the list outside the
li(<ul><li>A</li><ul>…</ul></ul>): the sublist must go inside theliit belongs to, before its</li>. - Forgetting to close the
lielements in nested lists: the browser will try to correct it, sometimes in unexpected ways. Check your indentation. - Using lists to indent text: if it isn't a series of items, it isn't a list; indentation is done with CSS.
- Using
dlfor anything with two columns: only if it's a name–description relationship. For tabular data, use tables. - Tip: with Emmet you can generate lists quickly. Type
ul>li*4and pressTabto get aulwith fourlielements.
Exercises
Exercise 1. State which type of list you would use (ul, ol, dl or none) and why:
- The ingredients of the lemon sponge cake.
- The instructions for assembling the cafetière sold in the shop.
- The frequently asked questions: "Do you have gluten-free options?" and its answer, and so on.
- The story of how The Blue Cup was founded.
- The podium of a barista competition.
Exercise 2. This code has three errors. Find and correct them:
<ol>
<h3>Steps</h3>
<li>1. Preheat the oven.</li>
<li>Mix the ingredients.</li>
<ul>
<li>Flour</li>
<li>Sugar</li>
</ul>
<li>Bake for 40 minutes.</li>
</ol>Exercise 3. In menu.html, after "Coffees", add a "Bakery" section with a nested list: a "Pastries" category (butter croissant €2.40, chocolate croissant €2.30) and a "Breads" category (sourdough bread €3.00, rye bread €3.50). Then number the steps in this list starting from 7, because it continues an earlier one: "Leave to cool", "Cut into portions".
Solutions
Solution 1.
ul: the order of the ingredients doesn't change the recipe (if there were an order for adding them, that would be a different list: the steps).ol: the steps must be followed in order.dl: each question (dt) has its answer (dd).- None: it's a narrative text; paragraphs.
ol: the order (1st, 2nd, 3rd) is the main information.
Solution 2. Errors: (1) the h3 is a direct child of ol; (2) the number "1." has been typed by hand; (3) the nested ul is outside an li.
<h3>Steps</h3>
<ol>
<li>Preheat the oven.</li>
<li>Mix the ingredients:
<ul>
<li>Flour</li>
<li>Sugar</li>
</ul>
</li>
<li>Bake for 40 minutes.</li>
</ol>Solution 3.
<h2>Bakery</h2>
<ul>
<li>Pastries
<ul>
<li>Butter croissant: €2.40</li>
<li>Chocolate croissant: €2.30</li>
</ul>
</li>
<li>Breads
<ul>
<li>Sourdough bread (loaf): €3.00</li>
<li>Rye bread: €3.50</li>
</ul>
</li>
</ul>With start="7" the items are numbered 7 and 8. Another option would be <li value="7"> on the first item.
Conclusion
You now know how to choose and build the right list: ul when the order doesn't matter, ol when it does (with type, start, reversed and value to control the numbering) and dl with dt and dd for pairs of terms and descriptions. You know how to nest them correctly inside an li, to make sure their direct children are always list items and to recognise that a navigation menu is, at heart, a list of links. menu.html now has specialities, grouped coffees, the filter coffee steps and a glossary, and the temporary menu in index.html is now a list. In the next lesson, Block Quotes and Preformatted Text, you will give The Blue Cup's customers a voice with quotations and learn to display text that keeps its spaces and line breaks exactly as written.
HTML Course
Module 1: Introduction to HTML
- What Is HTML?
- Setting Up Your Work Environment
- Basic Structure of an HTML Document
- Tags, Elements and Attributes
- Creating Your First HTML Page
- File Organisation and Paths
Module 2: Text Formatting in HTML
- Headings and Paragraphs
- Text Formatting Tags
- Lists: Ordered, Unordered and Description Lists
- Block Quotes and Preformatted Text
- Block and Inline Elements: div and span
Module 3: Links and Media in HTML
- Creating Hyperlinks
- Special Links: Anchors, Email, Phone and Downloads
- Adding Images
- Embedding Videos
- Using the Audio Element
Module 4: Tables in HTML
- Basic Table Structure
- Table Head, Body and Foot
- Merging Cells: colspan and rowspan
- Accessible Tables: caption, scope and headers
- Styling Tables
Module 5: Forms in HTML
- Creating a Basic Form
- Form Elements: input, textarea and select
- HTML5 Input Types
- Labels and Grouping: label, fieldset, legend and datalist
- Form Attributes and Validation
- Submitting Forms
Module 6: HTML5 Semantic Elements
- Introduction to Semantic HTML
- header, nav and footer
- article, section and aside
- main and figure
- Other Semantic Elements: time, address, details and dialog
Module 7: Advanced HTML Techniques
- HTML Entities
- Using iframes
- Data Attributes
- Metadata: meta, SEO and Open Graph
- Microdata and Structured Data
- Graphics in HTML: SVG and canvas
Module 8: Integrating HTML and CSS
- Linking CSS to HTML
- Inline, Internal and External CSS
- Basic CSS Selectors
- The Box Model
- Styling HTML Elements
- Introduction to Layout with Flexbox and Grid
Module 9: Responsive Web Design
Module 10: Best Practices and Accessibility
- HTML Best Practices
- Validating HTML Code
- Web Accessibility Basics
- Using ARIA Roles and Attributes
- Accessibility Testing
- Performance and Resource Loading
