5 / 9 · Basic
Typography
WCAG suggests lines no longer than 80 characters and line spacing of at least 1.5 inside paragraphs. Two CSS properties handle both.
Jump to the 10 practice questionsYou will learn
- Write a font stack that ends in a generic family
- Set size, weight and a unitless line height
- Keep lines readable and finish with text-transform and letter-spacing
Before this: The box model
1Explore
Choosing fonts
font-family takes a list of fonts separated by commas. The browser uses the first one that is available on the reader's device and moves down the list if it is missing. Always end the list with a generic family, serif, sans-serif, monospace or system-ui, so that something sensible appears no matter what is installed. Font names that contain spaces go in quotes, as in "Noto Sans KR".
A font that is not on the device is skipped. The generic family at the end of the list is the safety net.
Tap a choice: the code and the preview change together.
<p style="font-family: Georgia, serif; font-size: 20px">Soda bread needs no yeast.</p>Nothing is drawn: there is no visible content.
2Problem
Why end with a generic family
Why end a font-family list with a generic family?
Hint
What happens when no listed font is installed?
Solution
Answer: So a suitable font appears even if none of the named ones is installed
The browser tries fonts in order, and a generic family supplies a fallback when the specifically named fonts are unavailable.
3Explore
Size, weight and line height
line-height sets the distance between lines. Give it a plain number with no unit, such as 1.6: it then means 1.6 times the font size of each element, so headings and small print each get spacing that fits them. A value with a unit, such as 24px, is calculated once and inherited as a fixed length, which is too tight for large headings.
Change the weight and the line height and compare how easy the lines are to follow.
Tap a choice: the code and the preview change together.
<p style="font-size: 18px; font-weight: 400; line-height: 1.6; max-width: 30ch">Soda bread needs no yeast and no waiting. Mix, shape, cut a cross and bake.</p>Nothing is drawn: there is no visible content.
4Problem
bold as a number
What does font-weight: bold equal?
Hint
Normal is 400.
Solution
Answer: 700
bold is the keyword for font weight 700. That value sets the thickness of the letters.
5Problem
Height of a line
font-size is 20px and line-height is 1.5. How tall is each line?
px
Hint
Multiply the font size by the line height.
Solution
Answer: 30 px
A unitless line-height multiplies the element’s font size, so the line height is 1.5 times 20 = 30 pixels.
6Explain
Line length and alignment
Very long lines are tiring: the eye loses its place when it jumps back to the start of the next line. WCAG 1.4.8 suggests no more than 80 characters per line (40 for Chinese, Japanese and Korean). The easy way to enforce this is max-width in ch units on paragraphs, for example max-width: 65ch. text-align sets alignment: left (or start) is easiest to read for long text in left-to-right languages, center suits short headings, and justify can open up uneven gaps between words.
<p style="max-width: 40ch">This paragraph stops at about 40 characters per line, so the eye finds the start of the next line easily.</p>
<p>This one runs as wide as its box allows, which is fine in a narrow preview and tiring on a wide screen.</p>7Problem
letter-spacing in em
text-transform: uppercase shows text in capitals without changing the HTML, so the source keeps its normal spelling and the style can be changed later in one place. MDN notes that long runs of capitals are harder to read for some people, including people with dyslexia, so keep it for short labels, and add a little letter-spacing, such as 0.08em.
font-size is 16px. How much is letter-spacing: 0.1em?
px
Hint
em multiplies the font size.
Solution
Answer: 1.6 px
em uses the element’s font size here, so multiply that size by the spacing factor: 0.1 times 16 = 1.6.
8Practice
A recipe card in type
Set body to font-family: Georgia, serif and line-height: 1.6. Make the h1 2.5rem (browsers already draw it at 2em, so the default does not count). Make .label uppercase with letter-spacing: 0.1em.
- font stack on body
- line-height 1.6 inherited
- h1 2.5rem
- uppercase label
- letter-spacing 0.1em
<style>
/* write your rules here */
</style>
<h1>Weekend Baking</h1>
<p class="label">Recipe of the week</p>
<p>Soda bread needs no yeast and no waiting. Mix flour, baking soda, salt and buttermilk, shape a round, cut a deep cross on top and bake it for about 40 minutes.</p>Hint
Three rules: body, h1 and .label. line-height takes a plain number.
A solution
<style>
body { font-family: Georgia, serif; line-height: 1.6; }
h1 { font-size: 2.5rem; }
.label { text-transform: uppercase; letter-spacing: 0.1em; }
</style>
<h1>Weekend Baking</h1>
<p class="label">Recipe of the week</p>
<p>Soda bread needs no yeast and no waiting. Mix flour, baking soda, salt and buttermilk, shape a round, cut a deep cross on top and bake it for about 40 minutes.</p>9Explain
Common mistakes
<style>
body { line-height: 20px; }
</style>
<h1>Weekend baking at the Corner Bakery</h1>
<p>Soda bread needs no yeast.</p><style>
body { line-height: 1.4; }
</style>
<h1>Weekend baking at the Corner Bakery</h1>
<p>Soda bread needs no yeast.</p>A line height in px is passed down as that length, so the big heading gets 20px lines and its lines overlap. A plain number scales with each font size.
10
Summary
- font-family is a fallback list ending in a generic family.
- Unitless line-height around 1.5 for body text; tighter for big headings.
- Limit line length with max-width in ch; uppercase needs a little letter-spacing.