Sub topic 11
Adapting to small and large screens
This chapter assumes you know what leading and measure are, and that you can set line-height and max-width in ch in CSS. Here you will make both values respond to screen size without breaking readability.
Why one setting is not enough
On a phone, the screen is narrow. A paragraph with a max-width of 65ch is simply as wide as the screen allows, so the measure can fall to 30 or 40 characters. On a large monitor, a paragraph with no limit can stretch past 120 characters. Both cases hurt reading. Short lines force the eye to jump to the next line too often. Very long lines make it hard to find the start of the next line.
The max-width cap already handles the large-screen problem. The small-screen problem needs a different approach, because you cannot add characters to a narrow screen. You can only adjust the other values around the text.
Let the cap do the work
A max-width is a ceiling, not a fixed width. The block shrinks below it when the container is narrow, so no media query is needed for the basic case.
body {
font-size: 1rem;
line-height: 1.5;
padding-inline: 1rem;
}
p {
max-width: 65ch;
}The padding-inline keeps text away from the screen edge. Without it, a narrow screen puts letters against the edge of the display.
Adjust leading with the measure
Leading and measure move together. Shorter lines tolerate tighter leading, and longer lines need more. You can use a media query to apply this when the screen is wide enough for the measure to reach its cap.
body {
font-size: 1rem;
line-height: 1.4;
padding-inline: 1rem;
}
p {
max-width: 65ch;
}
@media (min-width: 48em) {
body {
font-size: 1.125rem;
line-height: 1.6;
}
}Below 48em (768px at the default font size), lines are short, so 1.4 is enough. At and above it, the type grows slightly and the leading rises to 1.6. Because max-width is in ch, the cap grows with the font size and the measure stays near 65 characters.
Fluid values with clamp()
Instead of one breakpoint, clamp() lets the font size grow smoothly between a minimum and a maximum.
body {
font-size: clamp(1rem, 0.9rem + 0.5vw, 1.25rem);
line-height: 1.5;
}
p {
max-width: 65ch;
}Because line-height is unitless, the leading scales with the fluid font size automatically. You do not need a second clamp() for it.
Common errors
- Setting
width: 65chinstead ofmax-width: 65ch. A fixed width causes horizontal scrolling on narrow screens. - Using
pxforline-height. The leading then does not follow font size changes. - Forgetting side padding, so text touches the screen edge on phones.
- Making type very large on wide screens without raising leading, which makes the lines look cramped.
Diagram
Exercise
A paragraph uses max-width: 65ch and line-height: 1.5. On a 360px phone it shows about 35 characters per line. Which change helps most: lowering max-width to 35ch, or keeping it and checking side padding and font size? Explain.
Show answer
Keep max-width at 65ch. It is only a ceiling, and the screen is already the limit, so lowering it changes nothing on the phone and would shrink the measure on desktop. Instead, check that side padding is not wasting space and that the font size is not too large. A measure near 35 characters is acceptable on a phone, and the leading can stay near 1.4 to 1.5.
Exercise
Why does a unitless line-height work well with a fluid font size from clamp()?
Show answer
A unitless value is a multiplier of each element's own font size. When the font size changes, the line height changes with it, so the ratio stays the same and no second calculation is needed.