Sub topic 11
Putting a pairing into CSS
This chapter turns a pairing decision into working CSS. It assumes you can write basic CSS, know what a serif and a sans serif are, and have already chosen two typefaces with contrast and shared structure. Here the pair is Playfair Display for display text and Source Sans 3 for body text.
Give each typeface one role
Define each role once, as a custom property (a CSS variable). When you swap a font later, you change one line and the whole page follows.
:root {
--font-display: "Playfair Display", Georgia, serif;
--font-text: "Source Sans 3", system-ui, sans-serif;
}
h1, h2, h3 {
font-family: var(--font-display);
font-weight: 700;
}
body {
font-family: var(--font-text);
font-weight: 400;
}Each font stack ends with a generic family (serif or sans-serif). If the web font fails to load, the browser still falls back to the same classification, so the contrast you designed survives.
Match the apparent size across fonts
Two fonts set at the same font-size often look different in size. The cause is x-height, the height of the lowercase letters relative to the font size. A font with a large x-height looks bigger than one with a small x-height at the same size. This also happens between your chosen font and its fallback. When the fallback is swapped in, the text can change size and the layout can shift.
The font-size-adjust property scales each font so its x-height is a set fraction of the font size. A value of 0.5 means the x-height is about half the font size.
body {
font-family: "Source Sans 3", system-ui, sans-serif;
font-size-adjust: 0.5;
}
h1, h2, h3 {
font-family: "Playfair Display", Georgia, serif;
font-size-adjust: 0.5;
}Build contrast with more than the typeface
The serif and sans serif split is the main contrast. Size, weight, and line height can reinforce it. Large display text can sit tightly, with a line height around 1.15. Body text needs more room, around 1.6, to be readable across long lines.
h1 {
font-family: "Playfair Display", Georgia, serif;
font-size: 2.5rem;
font-weight: 700;
line-height: 1.15;
}
p {
font-family: "Source Sans 3", system-ui, sans-serif;
font-size: 1.125rem;
font-weight: 400;
line-height: 1.6;
}
.caption {
font-family: "Source Sans 3", system-ui, sans-serif;
font-size: 0.875rem;
font-weight: 600;
letter-spacing: 0.02em;
}Notice the caption. It is a supporting role, but it stays in the text family. It changes size, weight, and letter spacing instead of introducing a third typeface. That keeps the system to two faces.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Hard-coding font names in many rules | A font change means many edits and missed spots | Use custom properties for each role |
| Fallbacks with a different x-height | Text jumps in size when the web font loads | Use `font-size-adjust` and test the fallback |
| Using a generic family that does not match the font | The fallback loses the serif or sans serif contrast | End each stack with `serif` or `sans-serif` to match |
| Loading every weight of both families | Slower page loads | Load only the weights you use, such as 400, 600, and 700 |
| A third typeface for small elements | The pair becomes a trio and loses clarity | Vary size, weight, and spacing in the text family |
Exercise
Exercise
A page uses Playfair Display for headings and Source Sans 3 for body text. After the web fonts load, the body text looks slightly smaller than it did a moment before, and the layout shifts. What is the likely cause, and which CSS property helps?
Show answer
The fallback font (such as system-ui) has a different x-height than Source Sans 3, so the apparent size changes when the fonts swap. Setting font-size-adjust (for example 0.5) on the text and headings scales each font to a similar x-height, which reduces the visible jump.
Exercise
Your design needs a small label above each card. Should you add a third typeface for it? What would you do in CSS instead?
Show answer
No. Keep the label in the text family and vary size, weight, and letter spacing, for example font-size: 0.875rem, font-weight: 600, and letter-spacing: 0.02em. This keeps the system to two typefaces.