Sub topic 11
Putting a pairing into CSS
This chapter assumes you can write basic CSS, know what a font stack is, and have chosen a pair already: a display face for headings and a text face for body copy. It covers how to encode that decision so it is easy to change, how to keep the two faces visually matched, and how to handle fallbacks and loading.
Start with roles, not selectors
A pairing works because each typeface has one job. Your CSS should make those jobs explicit. Custom properties (CSS variables) let you name a role once and use it everywhere.
: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;
}Two things happen here. First, the variable names describe roles (display, text), not typeface names. If you later swap Playfair Display for another serif, you change one line. Second, body sets the text face, and every element inherits it unless a rule overrides it. Only headings need an explicit override. This keeps the number of places where a typeface is named small, which is the best defense against a third face creeping in.
Why each stack ends in a generic family
Each stack lists the web font, then a system fallback, then a generic family (serif or sans-serif). The browser walks the list left to right and uses the first font that is available. The generic family at the end guarantees a font of the right classification even if everything else fails. Choose fallbacks of the same classification as the primary face. A serif heading that falls back to a sans serif changes the contrast the pairing depends on.
Matching apparent size with font-size-adjust
Two fonts set at the same font-size often look different in size. The reason is x-height: the height of lowercase letters such as x, a, and e, relative to the font size. A font with a large x-height looks bigger than one with a small x-height at the same nominal size.
This matters in two situations:
- Your display and text faces have different x-heights, so one looks oversized next to the other.
- Your web font has not loaded yet, and the fallback has a different x-height. The text changes apparent size when the web font arrives, and the layout may shift.
The font-size-adjust property addresses both. It takes a ratio and scales each font so its x-height equals that ratio times 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;
}With font-size-adjust: 0.5, a font whose natural x-height is 0.45 of its size gets scaled up by about 11 percent. A font whose natural x-height is 0.55 gets scaled down by about 9 percent. The adjustment applies to whichever font in the stack is actually used, so the fallback is corrected too. That is the main value of the property for pairings.
Choosing the ratio
Pick a value close to the x-height ratio of your primary text face, so that face is changed as little as possible and the others are scaled toward it. You can measure the ratio in a font editor, or read the OS/2 table's sxHeight divided by unitsPerEm. Many typefaces fall between 0.45 and 0.52. If you set the ratio far from the primary face's real value, you will resize your main font and change your type scale.
A shorthand and a caveat
The property also accepts a metric keyword, such as ex-height 0.5, and the default metric is x-height. You can use from-font to take the ratio from the first available font. Be careful with from-font in a pairing: it removes the matching effect, because each font is then scaled to its own x-height, which leaves the differences in place.
Lowercase-free scripts and all-caps settings do not benefit from x-height matching. If a heading is set in all capitals, cap height is what the reader sees, and the property does not control it. Check those cases by eye.
Expressing contrast with more than the family
A pairing already has contrast in classification, here serif against sans serif. CSS lets you add contrast in size, weight, and line height. Keep these deliberate. Each gap between roles should be large enough to read as a decision.
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 choices:
- Line height shrinks as size grows. The heading uses 1.15 and the paragraph uses 1.6. Large type needs less leading, because the gaps between lines look larger as letters grow. Small text needs more to stay readable.
- The caption stays in the text family. It is a supporting role, and it does not need a third typeface. A smaller size, a heavier weight, and slight letter spacing set it apart.
- Letter spacing is positive for small text. A small amount (0.02em) opens up small sizes. Large display type usually wants zero or slightly negative spacing.
| Role | Family | Size | Weight | Line height |
|---|---|---|---|---|
| Display | Serif | 2.5rem | 700 | 1.15 |
| Text | Sans serif | 1.125rem | 400 | 1.6 |
| Supporting | Sans serif | 0.875rem | 600 | Inherited or about 1.4 |
Loading fonts without breaking the pairing
A pairing only helps if both faces appear when the reader needs them. Loading is where many pairings fail in practice.
@font-face {
font-family: "Source Sans 3";
src: url("/fonts/source-sans-3-var.woff2") format("woff2");
font-weight: 200 900;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "Playfair Display";
src: url("/fonts/playfair-display-var.woff2") format("woff2");
font-weight: 400 900;
font-style: normal;
font-display: swap;
}Points to understand:
font-display: swapshows the fallback immediately and swaps in the web font when it loads. Text is readable at once, at the cost of a visible change.font-size-adjustreduces how jarring that change is.- Variable fonts (a single file covering a range of weights, written as
font-weight: 200 900) reduce the number of requests. If your pairing uses several weights of one face, a variable file is usually smaller than separate static files. - Subsetting removes glyphs you do not need, such as unused scripts. This shrinks the file. Keep every glyph your content requires, including punctuation and numerals, since a missing character falls back to another font and breaks the pairing in the middle of a word.
- Preloading the font used above the fold, usually the display face and the regular text weight, starts the download earlier.
<link rel="preload" href="/fonts/source-sans-3-var.woff2" as="font" type="font/woff2" crossorigin>Fonts need the crossorigin attribute on preload even when they come from your own origin. Without it, the browser fetches the file twice.
How the pieces connect
Common implementation errors
- Naming typefaces in many places. If
font-family: "Playfair Display"appears in twenty rules, a swap is slow and error-prone. Use the variables and set the family once per role. - Fallbacks of the wrong classification. A heading stack ending in
sans-serifcollapses the serif and sans serif contrast when the web font fails. - Missing weights. If you ask for
font-weight: 600and only 400 and 700 are loaded, the browser picks the nearest, or synthesizes a bold. Synthesized styles distort letterforms. Load the weights you use, or use a variable font. - Setting
font-size-adjuston only one role. Adjusting the text face but not the display face leaves the mismatch the property was meant to remove. - Using
from-fontand expecting a match. It preserves each font's own proportions. - Relying on the fallback for headings you never tested. Throttle the network in browser developer tools and look at the swap.
Using a different weight of the same face
Not every pairing needs two families. A single family can supply both roles if weight and size carry the contrast. The CSS pattern is the same. Define the roles as variables, and point both at the same family with different weights.
:root {
--font-display: "Source Sans 3", system-ui, sans-serif;
--font-text: "Source Sans 3", system-ui, sans-serif;
}
h1, h2, h3 {
font-family: var(--font-display);
font-weight: 800;
}
body {
font-family: var(--font-text);
font-weight: 400;
}Keeping two variables even when they hold the same value preserves the role structure. You can later replace only the display face without editing any selectors.
Exercise
A page sets headings in Playfair Display with fallback Georgia, and body text in Source Sans 3 with fallback system-ui. Before the fonts load, the body text looks noticeably smaller than it does afterward. Which property fixes this, what value would you choose, and why should you apply it to both the heading and the body rules?
Show answer
Use font-size-adjust. Set the value near the x-height ratio of the primary text face, Source Sans 3, which is about 0.5, so that face changes little and the fallbacks are scaled toward it. The fallback system-ui font has a smaller x-height, so at the same font-size it looks smaller; the property scales whichever font is in use so the apparent size stays steady when the web font swaps in. Apply it to both rules because the display face and its fallback (Georgia) also differ in x-height from each other. Setting it on one role leaves that role's mismatch in place, and the two roles would no longer share one apparent scale.
Exercise
Write the custom properties and two rules that give a design a serif display role and a sans serif text role, then name one change you would make so that swapping the display typeface touches only one line.
Show answer
:root { --font-display: "Playfair Display", Georgia, serif; --font-text: "Source Sans 3", system-ui, sans-serif; } with h1, h2, h3 { font-family: var(--font-display); } and body { font-family: var(--font-text); }. Because every rule refers to the variable and not to the typeface name, replacing the display typeface only requires editing the value of --font-display, and the fallback stack with the correct generic family stays next to it.