Sub topic 14
Adapting hierarchy across screen sizes
This chapter assumes you know the type scale from earlier chapters (a 1rem body size with a 1.25 ratio), basic CSS, and the terms leading and measure. Here you will keep the hierarchy intact as the screen changes width.
Why a fixed scale breaks
A headline set at 2.441rem looks strong on a desktop. On a 320px phone it can wrap into five short lines and push the body text below the fold. If you shrink every size by the same amount to fix that, the gap between headline and body also shrinks, and the levels start to look alike.
The goal is not to keep every size. The goal is to keep the contrast between levels, which is what readers use to rank the text.
Body text stays put, headlines move
Body text is your baseline. Keep it near 1rem on every device so it stays readable. Let the headline do most of the changing, because it has the most room to move.
The clamp() function takes a minimum, a preferred value, and a maximum. The browser uses the preferred value, but never goes below the minimum or above the maximum.
h1 {
font-size: clamp(1.75rem, 1.2rem + 2.5vw, 2.75rem);
}
body {
font-size: 1rem;
}Here vw is 1% of the viewport width. On a 320px screen the preferred value is 1.2rem + 8px, about 1.7rem, so the minimum of 1.75rem applies. On a 1200px screen it is 1.2rem + 30px, about 3.08rem, so the maximum of 2.75rem applies. The headline stays between 1.75 and 2.75 times the body size, and the ratio never collapses.
Adjust leading and measure too
Smaller screens change more than size. A narrow column holds fewer characters per line, so the measure shrinks on its own. That is usually fine. Do not widen it to fill space on large screens, though. Keep the 65ch cap so lines stay comfortable.
Headline leading should also follow the size. A large headline looks best with tight leading, such as 1.15. At the small end, a headline that wraps to three or four lines needs a little more room, so you might use 1.25 there. You can set a custom property at a breakpoint to switch it.
:root {
--headline-leading: 1.25;
}
@media (min-width: 48rem) {
:root {
--headline-leading: 1.15;
}
}
h1 {
font-size: clamp(1.75rem, 1.2rem + 2.5vw, 2.75rem);
line-height: var(--headline-leading);
}
p {
max-width: 65ch;
}Common errors
- Using only
vwfor font size. Text then ignores the user's browser font setting and can become too small. Mixremwithvw, as in theclamp()above. - Scaling body text with the headline. This makes small screens hard to read and removes the baseline.
- Forgetting spacing. Subhead margins set in
emscale with the subhead's own font size, which is usually what you want. Margins in fixed pixels do not, so the grouping of heading and text can drift. - Checking only one width. Test at the narrowest and widest sizes you support, and at one size in between.
Check your result
At each width, run the squint test from the testing chapter. The headline should still read first, subheads second, and body text third. If two levels blur together on the phone, raise the headline minimum or lower the body size slightly, but not below 1rem.
Exercise
A headline uses font-size: clamp(1.5rem, 1rem + 3vw, 3rem) and the body is 1rem. What size does the headline have on a 400px-wide screen, and is it using the minimum, the preferred value, or the maximum? Assume 1rem = 16px.
Show answer
The preferred value is 1rem + 3vw = 16px + 12px = 28px, which is 1.75rem. This lies between the minimum (24px) and the maximum (48px), so the preferred value is used. The headline is 1.75 times the body size.
Exercise
Why is font-size: 5vw alone a poor choice for a headline?
Show answer
It has no minimum or maximum, so the text becomes tiny on small screens and huge on wide ones. It also ignores the user's browser font setting. Wrapping it in clamp() with rem limits fixes both problems.