Style a description list (dl) with CSS
9 A description list for pairs
fairLay out term-and-definition pairs, including one term with two definitions, and style them as two columns without using a table.
Stuck? Three hints, smallest first
- 1.
<dl>holds<dt>terms and<dd>descriptions; a term may have several descriptions. - 2. The pairs are siblings, not nested, so a two-column grid on the
<dl>lines them up. - 3.
<dd>has a browser default indent. Resetmarginbefore you lay it out.
<dl class="spec">
<dt>Departures</dt>
<dd>05:00 to 21:30, every day</dd>
<dt>Bays</dt>
<dd>Six, lettered A to F</dd>
<dt>Payment</dt>
<dd>Cash at the window</dd>
<dd>Transfer, with the slip shown at the bay</dd>
</dl>
.spec {
display: grid;
grid-template-columns: max-content 1fr; /* term column hugs its widest term */
gap: 6px 16px;
margin: 0;
max-width: 380px;
}
.spec dt {
font-weight: 600;
color: #17171a;
}
.spec dd {
margin: 0; /* browsers indent dd by 40px; reset it */
color: #5c5c66;
}
/* The second description of a term sits under the first, in the value column. */
.spec dd + dd {
grid-column-start: 2;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
test('a description list is used', hasTag(html, 'dl'), true);
test('three terms', countTag(html, 'dt'), 3);
test('four descriptions, because one term has two', countTag(html, 'dd'), 4);
test('the dd default indent is reset', /dd\s*\{[^}]*margin\s*:\s*0/.test(css), true);
test('the pairs are laid out as two columns', /grid-template-columns\s*:\s*max-content\s+1fr/.test(css), true);
test('the term column sizes to its content', /max-content/.test(css), true);
test('a repeated description is pushed to the value column', /dd \+ dd/.test(css), true);
test('no table was used for a non-tabular pairing', hasTag(html, 'table'), false);
Output
A <dl> is a list of groups, not of pairs, which is why one <dt> can legitimately own several <dd>. That is also why laying it out needs care: the elements are flat siblings, so dd + dd is how you tell a second description to start in the value column rather than the term column. max-content on the first track is the neat part — the column becomes exactly as wide as the longest term and no wider.