Sub topic 17
Description lists
A description list is the HTML structure for name-value pairs: a term and what it means, a label and its data, or a question and its answer. This chapter assumes you can write a ul and an ol and know how elements and attributes work. It covers the three elements of a description list, the allowed groupings, and the mistakes that produce invalid or confusing markup.
The three elements
A description list uses three elements:
dlis the container for the whole list.dt(description term) holds the name, term, or label.dd(description details) holds the value, definition, or explanation.
<dl>
<dt>HTML</dt>
<dd>The markup language used to structure web pages.</dd>
<dt>URL</dt>
<dd>The address of a resource on the web.</dd>
</dl>Browsers indent each dd by default, about 40 pixels from the start edge. That indent is only a default style, so you can change it with CSS. The meaning comes from the elements, not from the indent.
How terms and details group
The pairing is by position, not by nesting. A dd belongs to the dt elements directly before it. This gives three valid shapes.
| Shape | Markup pattern | Typical use |
|---|---|---|
| One term, one detail | `dt` then `dd` | A glossary entry |
| One term, several details | `dt` then several `dd` | A word with two meanings, or a person with several phone numbers |
| Several terms, one detail | several `dt` then `dd` | Synonyms that share one explanation |
<dl>
<dt>Color</dt>
<dt>Colour</dt>
<dd>The visual property produced by different wavelengths of light.</dd>
<dt>Phone</dt>
<dd>+1 555 555 0123 (office)</dd>
<dd>+1 555 555 0456 (mobile)</dd>
</dl>In the first group, "Color" and "Colour" are both terms for the one dd that follows them. In the second group, one term has two details.
The list must follow some ordering rules:
- The first child that is not a wrapper must be a
dt. A list cannot start with add. - The last child must be a
dd. Adtwith nothing after it is incomplete. - Each
dtgroup must be followed by at least oneddbefore the next group starts.
Grouping with div
Sometimes you want to style or target one pair as a unit, for example to add a border between entries. HTML allows a div as a direct child of dl, as long as each div wraps a complete group of one or more dt followed by one or more dd.
<dl>
<div class="entry">
<dt>Author</dt>
<dd>Ada Lovelace</dd>
</div>
<div class="entry">
<dt>Published</dt>
<dd><time datetime="1843-07-10">10 July 1843</time></dd>
</div>
</dl>The div has no meaning of its own here. It exists only as a styling and scripting hook. Do not mix wrapped and unwrapped groups carelessly. Both are valid, but one style per list is easier to maintain.
The following diagram shows which children a dl accepts and how they relate.
What goes inside dt and dd
A dt should hold a short name. Avoid putting headings, sections, or other dt elements inside it. Phrasing content such as text, strong, abbr, or code is the safe choice.
A dd accepts flow content, so it can hold paragraphs, links, images, and other lists. This makes it a good place for a longer explanation.
<dl>
<dt>Install</dt>
<dd>
<p>Download the package and run the setup program.</p>
<ul>
<li>Windows 10 or later</li>
<li>macOS 13 or later</li>
</ul>
<p><a href="/docs/install.html">Full installation guide</a></p>
</dd>
</dl>Note that the nested ul sits inside the dd, which follows the same rule you saw for nested lists: a child list goes inside an item, never directly inside the parent list element.
When to choose a description list
Use dl when the content is a set of name-value groups. Good fits:
- Glossaries of terms and definitions.
- Metadata blocks, such as author, date, and category on an article.
- Product specifications, such as weight, size, and color.
- FAQ pages where each question is a
dtand each answer is add.
Use another structure in these cases:
- If the items are plain entries with no label, use
ulorol. - If the data has several columns per row and needs column headings, use a table.
- If you only want an indented block of text, use CSS on a normal element. Do not use
ddfor indentation.
The HTML specification also says dl is not meant for marking up a conversation, where each dt is a speaker and each dd is a line of speech. Use paragraphs for dialogue. The same page warns against using dl only to get a two-column layout, because the markup should describe the content, not the appearance.
Accessibility notes
Screen readers handle description lists with mixed results. Many announce the list and its number of items, and some read each term and its details as a pair, but support has varied between readers and versions. Two habits help in every case:
- Keep each
dtshort and unique inside the list, so that the label alone makes sense when it is read aloud. - Keep the
dtandddas direct, adjacent siblings. Inserting unrelated elements between them breaks both validity and the reading order.
Common errors
| Mistake | Why it is wrong | Fix |
|---|---|---|
| Starting the list with `dd` | Every detail needs a term before it | Put a `dt` first |
| Wrapping each `dt`/`dd` pair in `li` | `li` is only valid inside `ul`, `ol`, or `menu` | Remove the `li` or use a `div` |
| Putting a `dt` and a `dd` in separate `div` elements | A `div` must hold a complete group | Put the whole group in one `div` |
| Using `dd` to indent a paragraph | The element claims a term-detail relationship that does not exist | Use CSS margin or padding |
| Leaving a `dt` with no `dd` | The term has no value | Add the `dd`, or drop the term |
A complete example
This page runs as shown and combines the patterns above: a metadata block with a wrapped group, several details for one term, and a link inside a detail.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Trail guide</title>
<style>
dt { font-weight: bold; }
dd { margin-inline-start: 1.5rem; }
</style>
</head>
<body>
<h1>Pine Ridge Trail</h1>
<dl>
<div>
<dt>Length</dt>
<dd>8.4 km</dd>
</div>
<div>
<dt>Trailheads</dt>
<dd>North gate</dd>
<dd>Lake parking lot</dd>
</div>
<div>
<dt>Map</dt>
<dd><a href="maps/pine-ridge.pdf">Download the trail map (PDF)</a></dd>
</div>
</dl>
</body>
</html>Exercise
The markup below has two errors. Name them and write the corrected list. <dl> <dd>A flowering plant</dd> <dt>Rose</dt> </dl>
Show answer
The dd comes before any dt, and the list ends with a dt that has no dd. The order is reversed. The corrected list is: <dl><dt>Rose</dt><dd>A flowering plant</dd></dl>. A term must come first, and every group of terms must end with at least one detail.
Exercise
You are marking up a product page with the fields Weight, Size, and Color. Weight has one value, and Color has two values (Red and Blue). Which elements do you use, and how many dd elements does Color need?
Show answer
Use one dl with a dt for each field name. Weight gets one dd, Size gets one dd, and Color gets two dd elements, one for Red and one for Blue, placed right after the Color dt. Optionally wrap each field's group in a div for styling.