Sub topic 10
Adjusting text across screen sizes
This chapter assumes you can write basic HTML and CSS selectors and are comfortable with font-size, rem, and custom properties. It also assumes you have a type scale like the one built earlier in this tutorial. The goal here is to keep that scale readable and in proportion on a 320px phone and a 1600px monitor. Every role in the hierarchy (headings, body text, captions, labels) must keep its relative rank at every width.
What should change with the screen, and what should not
Resizing text is not the same as resizing a layout. Most of your hierarchy should stay stable, and only a few values need to move.
- Heading sizes usually need the most adjustment. A 3rem
h1fills several lines on a narrow phone and looks timid on a wide desktop. - Body text barely changes. A range of about 1rem to 1.125rem works everywhere.
- Captions and labels should stay near their normal size. Shrinking them on small screens makes them harder to read exactly where reading is hardest.
- Line length is controlled by
max-widthinchunits and by container width, not by font size. - Ratios between levels should hold. If the
h1shrinks, theh2should shrink with it, so the headings do not collapse into the body text.
The simplest mental model is that the token values change and the rules that use them stay the same. If you wrote h1 { font-size: var(--text-2xl); }, you should not have to touch that rule when the screen changes.
Approach 1: Change tokens at breakpoints
The most predictable method is to redefine custom properties inside a media query. Write breakpoints in em so they respect the user's default font size. A user who sets a larger browser font reaches the wider layout sooner, which is usually what they want.
:root {
--text-sm: 0.875rem;
--text-base: 1rem;
--text-lg: 1.125rem;
--text-xl: 1.375rem;
--text-2xl: 1.75rem;
--leading-body: 1.5;
--leading-heading: 1.2;
}
@media (min-width: 40em) {
:root {
--text-lg: 1.25rem;
--text-xl: 1.5rem;
--text-2xl: 2.25rem;
}
}
@media (min-width: 64em) {
:root {
--text-xl: 1.75rem;
--text-2xl: 3rem;
}
}
body { font-size: var(--text-base); line-height: var(--leading-body); }
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); }Only the tokens change. The selectors never mention a screen size. The cost is that sizes jump at each breakpoint. Between 40em and 64em, a heading stays the same size while the window grows, then steps up suddenly.
Approach 2: Fluid sizes with clamp()
clamp(min, preferred, max) returns the preferred value, but never less than the minimum or more than the maximum. When the preferred value uses vw (1% of the viewport width), the text grows smoothly between the bounds.
h1 {
font-size: clamp(1.75rem, 1.2rem + 2vw, 3rem);
}This is the rule from the introduction to this topic. At a viewport of 440px, the preferred value 1.2rem + 2vw works out to 19.2px + 8.8px = 28px, which equals the minimum. Below that width, the minimum (1.75rem = 28px) wins. At 1440px it works out to 19.2px + 28.8px = 48px, which equals the maximum. Above that, the maximum (3rem) wins. Between those widths, the size grows steadily.
Computing the preferred value so the bounds land where you want
The rule above reaches its bounds at 440px and 1440px, which may not be the widths you intend. To make the growth span exact viewport widths, compute a line through two points. Suppose you want 28px at a 320px viewport and 48px at a 1280px viewport (assuming a 16px root size):
- Slope = (48 − 28) / (1280 − 320) = 20 / 960 = 0.020833 per px of viewport, which is 2.0833vw.
- Intercept = 28 − 0.020833 × 320 = 21.333px, which is 1.3333rem.
- Result:
clamp(1.75rem, 1.3333rem + 2.0833vw, 3rem).
h1 {
font-size: clamp(1.75rem, 1.3333rem + 2.0833vw, 3rem);
}Why the rem part matters
Never write a fluid size with vw alone, such as font-size: 4vw. Browser zoom and the browser's font-size setting scale rem values, but a pure vw value depends only on the window width. A user who zooms to 200% gets text that does not grow in the way they expect. Combining a rem term with a vw term keeps part of the size tied to the user's settings, and the rem bounds keep the extremes tied to them as well.
Choosing between the two approaches
| Question | Breakpoint tokens | clamp() |
|---|---|---|
| How do sizes change? | In steps at set widths | Smoothly between two widths |
| Easy to predict exact sizes? | Yes, at each breakpoint | Only after calculating |
| Reviewing in a design tool | Matches named frames | Needs the formula |
| Risk with zoom | Low | Moderate if the range is wide |
| Number of rules | One block per breakpoint | One line per token |
You can also combine them. Define fluid tokens with clamp() for the large sizes, where the change is noticeable, and keep small tokens such as --text-sm and --text-base as fixed rem values.
:root {
--text-sm: 0.875rem;
--text-base: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
--text-lg: clamp(1.125rem, 1.05rem + 0.4vw, 1.25rem);
--text-xl: clamp(1.375rem, 1.1rem + 1.1vw, 1.75rem);
--text-2xl: clamp(1.75rem, 1.3333rem + 2.0833vw, 3rem);
}Because every heading token is defined once, a change to the scale is a change to one block, and every selector that uses the tokens follows.
Keeping line length steady
When the font grows, a fixed pixel max-width gives a different number of characters per line. Use ch, which is the width of the "0" character in the current font, so the measure scales with the text.
p, li {
max-width: 65ch;
}On a phone, the container is narrower than 65ch, so the paragraph fills the screen minus its padding. On a desktop, the limit stops lines from stretching across the whole window. Add padding at the sides of the page so text never touches the screen edge:
main {
padding-inline: max(1rem, 4vw);
}Responding to the container, not the viewport
A card in a narrow sidebar on a wide screen needs small text. A viewport media query cannot tell. A container query can, because it measures the nearest ancestor you mark as a container.
.card {
container-type: inline-size;
}
.card h2 {
font-size: var(--text-lg);
}
@container (min-width: 30em) {
.card h2 {
font-size: var(--text-xl);
}
}The heading changes when the card is wider than 30em, wherever on the page the card sits. Use this for components reused in different layouts. Keep page-level headings on viewport-based tokens.
Edge cases that break the hierarchy on small screens
Long words and narrow columns
A large heading with a long word, such as "Internationalization", can overflow a 320px screen. Allow the word to break instead of scrolling sideways:
h1, h2, h3 {
overflow-wrap: break-word;
hyphens: auto;
}hyphens: auto only works when the page declares its language, for example <html lang="en">.
Form controls and mobile zoom
Some mobile browsers zoom the page when a user focuses a text field whose font size is below 16px. Labels and inputs should therefore not shrink below 1rem on small screens.
label,
input,
select,
textarea {
font-size: max(1rem, 16px);
}Controls also do not inherit the page's font by default, so setting font: inherit on them keeps them in line with the rest of your type.
Automatic text inflation
Mobile browsers sometimes enlarge text blocks that they think are too small. Without a viewport meta tag, a page is treated as a desktop layout and everything is shrunk, so the tag is required:
<meta name="viewport" content="width=device-width, initial-scale=1">Do not add maximum-scale=1 or user-scalable=no. They stop users from pinching to zoom, which fails accessibility checks. If a browser inflates text unexpectedly, you can set text-size-adjust: 100% on html, but only after you have confirmed the cause.
Captions that vanish
Captions use the smallest token. At a 0.875rem floor, a caption is 14px, which is readable. Do not apply the clamp() shrink to captions, because small text at the lower bound is where legibility fails first.
Testing the result
- Open your browser's responsive mode and check widths of 320, 375, 768, 1024, and 1440px.
- At each width, confirm that the order of sizes is still
h1>h2>h3> body > caption. No level should match or pass the one above it. - Zoom to 200% on a desktop-sized window. Text should reflow into one column without horizontal scrolling and without overlapping.
- Change the browser's default font size to a larger value, such as 24px, and reload. Headings, labels, and body text should all grow, because every size is in
rem. - Check each heading wraps in at most a few lines at 320px.
If step 4 shows text that does not change, search your CSS for px font sizes or a vw-only value.
Common mistakes
- Using
vwalone for font size, which ignores the user's font settings. - Writing breakpoints in
px, so users with larger default fonts get the wide layout too late. - Redefining heading sizes inside every media query instead of changing tokens once.
- Making headings fluid but leaving
line-heightfixed inpx. Use unitless values so the spacing follows the size. - Letting
clamp()shrink body text below 1rem on small screens. - Setting
user-scalable=noto "fix" layout problems.
Exercise
You want an h2 that is 22px at a 320px viewport and 32px at a 1280px viewport, with a 16px root size. Write the clamp() rule, showing how you get the preferred value.
Show answer
Slope = (32 − 22) / (1280 − 320) = 10 / 960 = 0.010417 per px, which is 1.0417vw. Intercept = 22 − 0.010417 × 320 = 18.667px, which is 1.1667rem. Rule: h2 { font-size: clamp(1.375rem, 1.1667rem + 1.0417vw, 2rem); } The bounds are 22px = 1.375rem and 32px = 2rem.
Exercise
A teammate sets body { font-size: 2.5vw; }. Give two reasons this is a problem and a replacement.
Show answer
The size depends only on the window width, so it ignores the user's browser font-size setting and does not respond as expected to zoom. It also has no lower or upper bound, so text is tiny on a phone and huge on a wide monitor. A replacement is body { font-size: clamp(1rem, 0.95rem + 0.25vw, 1.125rem); }.
Exercise
Your page uses a 40em media query to enlarge headings. Why is em better than px for that breakpoint?
Show answer
An em in a media query is relative to the browser's default font size. A user who has raised that default reaches the breakpoint at a narrower window, so the layout and type scale match their larger text. A px breakpoint ignores their setting.