Sub topic 10
Adjusting text across screen sizes
This chapter assumes you know basic HTML and CSS, including font-size, rem units, and custom properties. You should also have the type scale from the earlier chapters: a set of --text-* tokens used by headings, body text, captions, and labels.
Why text needs to adapt
A heading that looks balanced on a 1440px desktop monitor can dominate a 360px phone screen. It may wrap to four lines and push the content below the fold. The opposite problem also happens. A size chosen for a phone looks timid on a wide screen, and the hierarchy flattens.
The goal is to keep the same ordering of sizes (h1 larger than h2, h2 larger than h3, body larger than captions) at every width. Only the actual values change.
Start with a sensible default
Body text rarely needs to change between screen sizes. Browsers default to 16px, which reads well on phones and desktops. Leave --text-base at 1rem and let the user's settings do their work.
Most of the adjustment belongs to large text, mainly the h1 and sometimes the h2. Small text such as captions and labels should stay readable, so avoid shrinking it below 0.875rem.
Fluid sizing with clamp()
The clamp() function takes three values: a minimum, a preferred value, and a maximum. The browser uses the preferred value unless it falls outside the bounds.
h1 {
font-size: clamp(1.75rem, 1.2rem + 2vw, 3rem);
}Here is what each part does:
1.75remis the smallest the heading will ever be.1.2rem + 2vwis the preferred value.vwis one percent of the viewport width, so this part grows as the window widens.3remis the largest size.
The preferred value mixes rem and vw on purpose. A value written only in vw ignores the user's font-size setting, so people who raised their browser's default size would see no change. The rem part keeps the text responsive to that setting.
Putting clamp() into the tokens
Because the scale lives in custom properties, you can make the large steps fluid and leave the small ones fixed. Every element that uses a token then adapts without further changes.
:root {
--text-sm: 0.875rem;
--text-base: 1rem;
--text-lg: clamp(1.125rem, 1.05rem + 0.4vw, 1.25rem);
--text-xl: clamp(1.25rem, 1.1rem + 0.8vw, 1.5rem);
--text-2xl: clamp(1.75rem, 1.2rem + 2vw, 3rem);
--leading-body: 1.5;
--leading-heading: 1.2;
}
h1 { font-size: var(--text-2xl); line-height: var(--leading-heading); }
h2 { font-size: var(--text-xl); line-height: var(--leading-heading); }
h3 { font-size: var(--text-lg); line-height: var(--leading-heading); }
p { max-width: 65ch; }Note that max-width: 65ch already adapts. On a narrow screen the paragraph is narrower than 65 characters, so it fills the width. On a wide screen the limit keeps lines readable.
Media queries as an alternative
You can also change tokens at breakpoints. This gives you exact control but produces visible jumps in size.
:root {
--text-2xl: 1.75rem;
}
@media (min-width: 48em) {
:root {
--text-2xl: 2.5rem;
}
}Use em in the query so the breakpoint also responds to the user's font size. The table compares the two approaches.
| Approach | Behavior | Best for |
|---|---|---|
| `clamp()` | Smooth growth between bounds | Headings and display text |
| Media query | Step change at a breakpoint | Layout-linked changes, a few fixed sizes |
How it fits together
Common errors
- Using
vwalone. The text does not respond to user settings. Add aremterm. - Bounds too close together. If the minimum and maximum are nearly equal, the heading never visibly changes. Leave a clear gap, such as 1.75rem to 3rem.
- Collapsing the hierarchy. If h2 and h3 are both clamped near the same value on small screens, they look identical. Check that each level stays distinct at the narrowest width.
- Shrinking captions and labels. Fluid sizing is for large text. Keep small text at or above
0.875rem. - Forgetting line height. A large heading that wraps needs the tighter
--leading-headingvalue, or the lines look loose.
Testing
Resize the browser window slowly from 320px to 1600px and watch the headings. Then raise the browser's default font size to 200% and confirm everything grows. Check that heading order stays visually clear at both ends.
Exercise
A teammate sets h1 { font-size: 5vw; } so the heading scales with the window. Name one problem with this, and rewrite the declaration so it has a minimum of 1.75rem, a maximum of 3rem, and still responds to the user's font-size setting.
Show answer
The text is tied only to viewport width, so it ignores the user's font-size preference, may not enlarge properly when zoomed, and becomes tiny on narrow screens and huge on wide ones. A fix is font-size: clamp(1.75rem, 1.2rem + 2vw, 3rem);. The rem term in the preferred value keeps it responsive to user settings, and the bounds stop it from getting too small or too large.
Exercise
Why should captions and labels usually stay a fixed size such as 0.875rem or 1rem, rather than use clamp() with a wide range?
Show answer
They are small text that must stay readable on every screen. Shrinking them on phones hurts legibility, and growing them on large screens competes with headings and weakens the hierarchy. Fixed rem sizes keep them stable and still respect user settings.