Sub topic 17
Description Lists
This chapter assumes you can write basic HTML elements and attributes, and that you already know ul, ol, and li from the earlier chapters on lists. Here you will learn the third list type, the description list.
What a description list is
A description list pairs terms with their descriptions. Use it when your content is a set of name and value pairs, such as a glossary, product details, or metadata. Three elements make it up:
dlis the container for the whole list.dtis a description term, the name being described.ddis a description details element, the text that describes the term.
<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 usually show each dd on its own line, indented under its dt. You can change that with CSS.
Terms and descriptions are groups
The pairing is flexible. One term can have several descriptions, and several terms can share one description.
<dl>
<dt>Coffee</dt>
<dd>A hot drink made from roasted beans.</dd>
<dd>The beans themselves, sold by weight.</dd>
<dt>Colour</dt>
<dt>Color</dt>
<dd>The visual property of an object. The first spelling is common in British English and the second in American English.</dd>
</dl>A dt or dd must sit inside a dl. Each group must start with at least one dt and be followed by at least one dd.
Grouping with div
If you want to style each term and description pair as a unit, wrap the pair in a div. This is the only wrapper element allowed directly inside a dl.
<dl>
<div>
<dt>Author</dt>
<dd>Ada Lovelace</dd>
</div>
<div>
<dt>Published</dt>
<dd>1843</dd>
</div>
</dl>The div has no meaning of its own. It only gives CSS, such as a grid or flexbox layout, one box per pair.
Choosing the right list
Common errors
- Putting
dtordddirectly in aulorol. They are only valid insidedl. - Wrapping each pair in a
span,p, orli. Onlydivis allowed as a wrapper. - Using a
dljust to indent text. Indentation is a styling job for CSS. - Starting a group with a
dd, or ending one with adt, so a term has no description.
Exercise
Exercise
You are marking up a product page with the details "Weight: 1.2 kg", "Colour: Red", and "Colour: Blue" is not wanted, so list "Colours" with two values, red and blue. Write the HTML with a dl, and group each pair in a div.
Show answer
<dl> <div> <dt>Weight</dt> <dd>1.2 kg</dd> </div> <div> <dt>Colours</dt> <dd>Red</dd> <dd>Blue</dd> </div> </dl> The term "Colours" has two dd elements, which is allowed. Each div wraps one term and its descriptions.