Part 1: Why typography matters (2 min)
Most of a web page is text, so good typography does more for a design than almost anything else. Compare these two, which have the same words:
/* Hard to read */
p { font-size: 11px; line-height: 1; color: #9ca3af; }
/* Comfortable */
p { font-size: 1rem; line-height: 1.6; color: #1f2937; }You'll learn how to control the typeface, size, spacing and alignment, and how to avoid common readability problems.
Part 2: Fonts and font stacks (5 min)
font-family
body {
font-family: Georgia, "Times New Roman", serif;
}This is a font stack: a list in order of preference. The browser uses the first font that's available on the user's device, then moves along the list if not.
Three rules:
-
Always end with a generic family:
serif,sans-seriformonospace. It's the safety net. -
Put quotation marks around font names that contain spaces, such as
"Times New Roman". - Separate fonts with commas.
| Generic family | Looks like | Examples |
|---|---|---|
serif | Small strokes on letter ends | Georgia, Times New Roman |
sans-serif | Clean, no strokes | Arial, Verdana |
monospace | All characters the same width | Courier New |
system-ui | The device's own interface font | Segoe UI, San Francisco |
Why a stack?
Not every device has every font. A visitor on a different system may not have the font you chose, and the fallback is what they'll see.
body {
font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}This is a popular "system font" stack. It uses the font the user is already used to, loads instantly (nothing to download), and looks right on every device. css_lesson5_font_stacks.csv has 17 stacks you can copy.
Quick check A. What's wrong with font-family: Trebuchet MS, sans-serif;? Will it work?
Quick check B. Why should every font stack end with a generic family?
Part 3: Sizing text (6 min)
The units
| Unit | Relative to | Use for |
|---|---|---|
px | A fixed CSS pixel | Borders and fine details |
rem | The root (html) font size, usually 16px | Font sizes and spacing (recommended) |
em | The current element's font size | Spacing that scales with its own text |
% | The parent's value | Widths |
rem: the one to use
Browsers default to 16px. So:
| rem | px (at 16px base) |
|---|---|
0.875rem | 14px |
1rem | 16px |
1.25rem | 20px |
1.5rem | 24px |
2rem | 32px |
body { font-size: 1rem; }
h1 { font-size: 2.25rem; }
small { font-size: 0.875rem; }Why not just px? People can set a larger default text size in their browser. Sizes in rem respect that setting and scale up. Sizes in px can ignore it, which is a real accessibility problem for people with low vision.
em, and why it can surprise you
em is relative to the parent's font size when used for font-size. Nested values multiply:
.box { font-size: 1.5em; } /* 1.5 × 16 = 24px */
.box p { font-size: 1.5em; } /* 1.5 × 24 = 36px */That compounding is why rem is safer for font sizes. em is great for spacing that should grow with its own text:
.button {
font-size: 1rem;
padding: 0.5em 1em; /* scales if you change the font-size */
}Quick check C. Convert to px at the default 16px base: a) 0.75rem b) 1.5rem c) 3rem.
Quick check D. .a { font-size: 2em; } contains .b { font-size: 2em; }. If the page base is 16px, what size is the text in .b?
Part 4: Spacing and hierarchy (6 min)
line-height
The space between lines of text. It's the single biggest readability setting.
body { line-height: 1.6; }Use a unitless number. 1.6 means 1.6 times the element's own font size, and it inherits as a number, so each child recalculates against its own size. A value like 1.6em is computed once and passed down as a fixed length, which can squash text inside children with bigger fonts.
| Text | Good line-height |
|---|---|
| Body paragraphs | 1.5 to 1.7 |
| Large headings | 1.1 to 1.3 |
Spacing between letters and words
.label {
letter-spacing: 0.08em;
}Slight extra letter spacing helps small uppercase text. Don't change it for normal paragraphs.
font-weight and font-style
strong { font-weight: 700; } /* bold */
.light { font-weight: 300; } /* only works if the font has a light weight */
em { font-style: italic; }Weights run from 100 to 900. 400 is normal and 700 is bold. Keywords normal and bold also work. A font may only have some of the weights, in which case the browser picks the nearest.
text-align
.centred { text-align: center; }Values: left, right, center, justify. Left-align long text. Centred paragraphs are hard to scan because each line starts in a different place.
A heading hierarchy
Readers scan headings first, so differences in size should be clear:
h1 { font-size: 2.25rem; line-height: 1.2; }
h2 { font-size: 1.75rem; line-height: 1.3; }
h3 { font-size: 1.25rem; line-height: 1.4; }
p { font-size: 1rem; line-height: 1.6; }Browsers already give headings default sizes and margins, as you saw in Lesson 3. You're overriding them for a consistent look.
Quick check E. Which is better for line-height on body text: 1.6, 1.6em or 26px? Why?
Quick check F. Which text-align is best for a long article, and why?
Part 5: Decorating text (4 min)
text-decoration
Mostly used on links, which are underlined by default.
a {
color: #1d4ed8;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}Other values include underline, line-through and overline.
Accessibility note. If you remove underlines, links must still be obviously different from surrounding text. Colour alone isn't enough for everyone. Keeping underlines (or adding them on hover and focus) is the safest choice.
text-transform
.label { text-transform: uppercase; }
.name { text-transform: capitalize; }The HTML text itself doesn't change, only how it's displayed. That means a screen reader still reads the original.
The font shorthand (optional)
p {
font: italic 700 1rem/1.5 Georgia, serif;
}The order is: style, weight, size/line-height, family. Size and family are required, and anything you leave out resets to its default, which can wipe other settings. Beginners should use the separate properties until they're confident.
Quick check G. Write CSS for a small label: uppercase, bold, 0.8rem, with 0.08em letter spacing.
Part 6: Web fonts with Google Fonts (4 min)
System fonts are fast, but if you want a distinctive look, you can load a web font. Google Fonts is free and the usual starting point.
Steps:
- Visit Google Fonts and choose a font, for example Open Sans.
- Select the weights you need, such as Regular 400 and Bold 700.
-
Copy the
<link>code Google gives you. -
Paste it into the
<head>of your HTML, before your own stylesheet. - Use the font name in your CSS, with a fallback.
<head>
<meta charset="UTF-8">
<title>My page</title>
<!-- paste the link tags from Google Fonts here -->
<link rel="stylesheet" href="style.css">
</head>body {
font-family: "Open Sans", Arial, sans-serif;
}I've left the Google link as a placeholder because the exact URL depends on the font and weights you pick, and Google generates it for you.
Tips:
- Load only the weights you use, since each one adds to the download.
- Always keep a fallback stack, in case the font fails to load.
- If a font doesn't appear, check the spelling, the link placement, and the Network tab in DevTools to see whether the request failed.
- Two fonts is plenty.
Quick check H. Where does the Google Fonts <link> go, and why before style.css?
Part 7: Readable text and pairing (3 min)
Line length
Lines that are too long are tiring because your eye loses its place. Aim for roughly 45 to 75 characters per line. The ch unit makes this easy. One ch is about the width of a zero in the current font.
.article {
max-width: 65ch;
margin: 0 auto;
}Pairing fonts
A reliable approach:
- One font for headings, one for body, or just one font in different weights.
- Pair a serif with a sans-serif, rather than two similar fonts.
- Keep styles consistent. Use the same heading font for all headings.
body { font-family: "Open Sans", Arial, sans-serif; }
h1, h2, h3 { font-family: Georgia, "Times New Roman", serif; }Because of inheritance (Lesson 3), you set the body font once and override only where needed.
A readable-text recipe
body {
font-family: system-ui, Arial, sans-serif;
font-size: 1rem;
line-height: 1.6;
color: #1f2937;
}
.article {
max-width: 65ch;
}Common mistakes
| Mistake | Fix |
|---|---|
font-family: Trebuchet MS, sans-serif | Quote names with spaces: "Trebuchet MS" |
| No generic family at the end | Add serif, sans-serif or monospace |
font-size: 12px on body text | Use at least 1rem (16px) |
line-height: 1 on paragraphs | Use 1.5 to 1.7 |
line-height: 1.6em | Use the unitless 1.6 |
Nested em sizes growing out of control | Use rem for font sizes |
| Text stretching across a wide screen | Set max-width: 65ch |
| Google Font link placed after your CSS | Place it before style.css |
| Too many fonts and weights | Limit to two fonts and the weights you use |
| Removing link underlines with no replacement | Keep links clearly distinct |
| Light grey text | Check contrast (Lesson 4) |
Key takeaways
- A font stack lists fonts in order of preference and ends with a generic family.
- Use rem for font sizes so they respect the user's settings.
- Line-height should be unitless, around 1.5 to 1.7 for body text.
- Weights run from 100 to 900, with 400 normal and 700 bold.
-
Left-align long text and keep lines to about 65 characters with
max-width: 65ch. -
text-decorationandtext-transformchange how text looks, not what it says. -
Google Fonts needs a
<link>in the head, before your CSS, plus a fallback stack. -
Set typography once on
bodyand let it inherit.
Exercises (15 min guided, plus extras)
1. Convert (2 min). At a 16px base, convert to px: a) 0.875rem b) 1.25rem c) 2.5rem. Then convert to rem: d) 20px e) 8px.
2. Spot the errors (3 min). There are five problems in this CSS:
body {
font-family: Open Sans, Arial;
font-size: 12px;
line-height: 1;
}
h1 {
font-size: 2em
text-align: centre;
}
a {
text-decoration: none
}3. Predict (2 min). With html at 16px, what size is the text in .inner?
<div class="outer"><div class="inner">Hi</div></div>.outer { font-size: 1.5rem; }
.inner { font-size: 0.5em; }4. Choose (2 min). For each, pick the better option and say why:
-
a) body
line-height:1.6or1 -
b) body paragraphs:
text-align: justifyorleft -
c) a long article:
max-width: 65chor none
5. Build it: an article page (12 min). Create article.html and article.css.
-
Write an article with an
<h1>, a short intro<p>, two<h2>sections each with a paragraph, and a closing paragraph. Use any text. -
Set a font stack,
font-size,line-heightandcoloronbody. -
Wrap the content in
<main class="article">withmax-width: 65chand centred withmargin: 0 auto. -
Style
h1,h2with a clear size hierarchy and a different font family from the body. -
Add a small uppercase label above the title using
text-transform,letter-spacingandfont-weight. - Style links with no underline and an underline on hover.
- Optional: add a Google Font and use it for headings.
- Resize your browser window and see how the lines stay readable.
6. Challenge (4 min). Open css_lesson5_units_reference.csv. Pick three units that are new to you, then try each one in a test page and write what you notice in the Notes column of css_lesson5_practice_tracker.csv.
Lesson 5 checkpoint
- I can write a font stack ending with a generic family.
-
I can convert between
pxandremat a 16px base. -
I know why
remis better thanpxfor font sizes. -
I can set a comfortable unitless
line-height. - I can style a heading hierarchy.
-
I can use
text-transformandletter-spacingon a label. - I can add a Google Font with a fallback.
-
I can limit line length with
ch.
⬇️ ANSWER KEY (only scroll once you've tried the questions)
Quick check A: It may not work as intended. The font name contains a space, so it should be quoted: "Trebuchet MS". Without quotes, the browser is likely to read it as separate words, so you should rely on quotes.
Quick check B: It's the safety net. If none of your named fonts is available, the browser at least picks a font of the right kind (serif, sans-serif, monospace) rather than its default.
Quick check C: a) 12px b) 24px c) 48px.
Quick check D: 64px. .a is 2 × 16 = 32px, and .b is 2 × 32 = 64px, because em multiplies when nested.
Quick check E: 1.6. A unitless value is recalculated for each element based on its own font size. 1.6em and 26px are fixed lengths computed once, so children with larger text could end up with lines too close together.
Quick check F: left. Every line starts in the same place, so the eye finds the next line easily. Centred text has ragged starts.
Quick check G:
.label {
text-transform: uppercase;
font-weight: 700;
font-size: 0.8rem;
letter-spacing: 0.08em;
}Quick check H: It goes in the <head>, before style.css. That way the font is requested before your CSS refers to it, and your rules can use it as soon as the page starts rendering.
Exercise 1:
- a) 14px
- b) 20px
- c) 40px
- d) 1.25rem
- e) 0.5rem
Exercise 2: The five problems:
-
Open Sansneeds quotes:"Open Sans". -
The stack has no generic family at the end, such as
sans-serif. -
font-size: 12pxis too small for body text and usespx. Use1rem. -
line-height: 1is too tight. Use about1.6. -
font-size: 2emis missing a semicolon.
text-align: centre is also wrong (it must be center), and text-decoration: none is missing a semicolon, which isn't an error before the closing brace but is a good habit. That's more than five problems, so don't worry if you found a different five. The key ones are the quotes, the missing generic family, centre, the missing semicolon after 2em, and the tight line height.
Corrected:
body {
font-family: "Open Sans", Arial, sans-serif;
font-size: 1rem;
line-height: 1.6;
}
h1 {
font-size: 2.25rem;
text-align: center;
}
a {
text-decoration: none;
}Exercise 3: 12px. .outer is 1.5 × 16 = 24px. .inner is 0.5 × 24 = 12px, since em is relative to the parent.
Exercise 4:
-
a)
1.6: lines need room to breathe. -
b)
left: justified text can create uneven gaps between words. -
c)
max-width: 65ch: keeps lines at a comfortable length.
Exercise 5: Check that the body font, size and line-height are set once on body, the article is centred at a width of about 65 characters, headings have a clear size order and a different font, the label is uppercase with letter spacing, and links underline only on hover.
Exercise 6: Your notes are personal. For example, ch is a good unit for line length, and vw makes text scale with the window, so it can get too small or large on its own.