Sub topic 10
Responsive type and spacing
This chapter assumes you can write CSS with rem, px, and %, that you know the media query and mobile-first material from earlier chapters, and that you can set text styles in a design tool such as Figma. You will learn how to make type and spacing scale between your mobile and desktop frames. You will also see where scaling breaks down.
Why type and spacing need their own treatment
A layout can reflow correctly and still feel wrong. A 3rem heading that suits a 1440px desktop frame can fill half of a 360px phone screen. Padding that looks calm on desktop wastes space on mobile. Text that looks right on mobile looks timid on a wide screen.
You have two ways to handle this:
- Stepped: change the value at a breakpoint with a media query. The value jumps.
- Fluid: compute the value from the viewport width so it changes smoothly between two limits.
Both are valid. Stepped values are easy to match to design frames. Fluid values avoid awkward in-between widths. Most production systems use fluid values for headings and large spacing, and fixed values for body text and small gaps.
Start with relative units
Set type in rem, not px. A rem is relative to the root font size, which defaults to the user's browser setting (usually 16px). Users who raise their default font size then get larger text. If you write px values, you override that preference.
| Unit | Relative to | Good for | Watch out for |
|---|---|---|---|
| `rem` | Root font size | Font sizes, most spacing | None for most uses |
| `em` | Current element's font size | Padding that should scale with its own text, such as buttons | Compounds when nested |
| `vw` | 1% of viewport width | The fluid part of a `clamp()` | Ignores user font settings and zoom when used alone |
| `ch` | Width of the "0" glyph | Line length (`max-width: 65ch`) | Depends on the font |
| `px` | Fixed CSS pixel | Hairline borders, shadows | Does not respect user font settings |
Leave the root font size alone. Do not set html { font-size: 62.5% } or a pixel value. It makes rem arithmetic easier, but it can break the user's preference if you get it wrong.
Stepped type: the mobile-first version
This is the simplest approach and follows the pattern from the mobile-first chapter. The base style is the mobile size, and each min-width query raises it.
h1 {
font-size: 1.75rem;
line-height: 1.2;
}
@media (min-width: 48em) {
h1 { font-size: 2.25rem; }
}
@media (min-width: 80em) {
h1 { font-size: 3rem; }
}These three sizes map directly to the mobile, tablet, and desktop text styles in your design file. The cost is that a heading at 47em is the same size as one at 20em, then jumps by 0.5rem at 48em.
Fluid type with clamp()
clamp(min, preferred, max) returns the preferred value, but never less than min or more than max.
h1 {
font-size: clamp(1.75rem, 1.25rem + 2vw, 3rem);
}With a 16px root, the minimum is 28px and the maximum is 48px. The preferred value is 20px + 2vw. It reaches 28px when 2vw = 8px, which is a 400px viewport. It reaches 48px when 2vw = 28px, which is a 1400px viewport. Below 400px the heading stays at 28px, and above 1400px it stays at 48px.
The chart below plots that behavior.
Deriving the numbers from your frames
You do not need to guess the preferred value. Take the heading size from your mobile frame and the size from your desktop frame, and choose the viewport widths where the scaling should start and stop. Then:
- Slope = (maxSize − minSize) / (maxWidth − minWidth). For the example: (48 − 28) / (1400 − 400) = 0.02, which is
2vw. - Intercept = minSize − slope × minWidth. For the example: 28 − 0.02 × 400 = 20px, which is
1.25rem. - Write the preferred value as
intercept + slope, here1.25rem + 2vw.
Record the result as a design token so the design file and the CSS agree.
:root {
--step-body: 1rem;
--step-h2: clamp(1.375rem, 1.1rem + 1.2vw, 2rem);
--step-h1: clamp(1.75rem, 1.25rem + 2vw, 3rem);
}
h1 { font-size: var(--step-h1); }
h2 { font-size: var(--step-h2); }
body { font-size: var(--step-body); }Keep body text steady
Body text is the one place where fluid scaling usually does not pay off. Readers need a comfortable size on every device, and 1rem works on a phone and on a monitor. Instead of enlarging body text, control the reading experience through line length and line height.
p {
max-width: 65ch;
line-height: 1.5;
}- Line length: aim for roughly 45 to 75 characters per line. On wide screens, a
max-widthinchprevents very long lines even when the layout has plenty of room. On narrow screens, the container is narrower than the limit, so the limit has no effect. - Line height: use a unitless value. A unitless
1.5is multiplied by each element's own font size, so it adapts when headings scale. Large headings usually need a tighter value (1.1 to 1.25) than body text.
Responsive spacing
Spacing follows the same rules as type. Use rem for padding, margins, and gaps so they scale with the user's font setting. Use a small, named scale instead of one-off values.
:root {
--space-xs: 0.5rem;
--space-s: 1rem;
--space-m: clamp(1rem, 0.6rem + 1.5vw, 2rem);
--space-l: clamp(2rem, 1rem + 4vw, 5rem);
}
.section {
padding-block: var(--space-l);
padding-inline: var(--space-m);
}
.stack > * + * {
margin-block-start: var(--space-s);
}Small spacing (--space-xs, --space-s) stays fixed because a 1rem gap between a label and its field should not grow with the screen. Larger spacing (--space-m, --space-l), such as section padding and page gutters, benefits from scaling because it is the space that most visibly feels cramped on mobile and empty on desktop.
The .stack > * + * pattern (the "lobotomized owl") adds space between siblings only, so you never have to remove a trailing margin on the last item.
Mapping this to your design frames
In the design tool, create text styles and spacing values that match your tokens, not one-off sizes on each frame.
- Define each text style once per frame size, for example
Heading/XLwith a mobile value of 28px and a desktop value of 48px. In Figma, variables with modes (one mode per frame size) do this well. - Give every style the same name as its CSS token, such as
step-h1, so developers can find it. - For fluid values, specify the two end values and the width range where scaling happens. The tablet frame will show an in-between value, so label it as an example and not as a third fixed size.
- Check the design at the widths between frames. A fluid heading at 800px is 36px, which appears in no frame.
Common errors
- Setting font sizes in
px. This ignores user preferences. Userem. - Using
vwwith noremterm. Zoom and font settings stop working. - Scaling body text with the viewport. Body text then becomes too small on narrow phones or too large on wide screens. Keep it at about 1rem.
- Forgetting line length. A heading that scales well still sits above paragraphs that run 120 characters wide on a large monitor.
- Too many sizes. Each extra step adds a design decision and a token. Five to seven steps cover most interfaces.
- Fixed heights on text containers. When type grows, text overflows its box. Use
min-heightor no height.
Exercise
Your design has a page title of 32px on the mobile frame and 56px on the desktop frame. You want the title to scale linearly from a 360px viewport to a 1280px viewport. Write the clamp() value, assuming a 16px root. (Round the slope to two decimal places of vw.)
Show answer
Slope = (56 − 32) / (1280 − 360) = 24 / 920 ≈ 0.0261 px per px, so about 2.61vw. Intercept = 32 − 0.0261 × 360 ≈ 22.6px ≈ 1.41rem. The value is clamp(2rem, 1.41rem + 2.61vw, 3.5rem). The minimum is 32px = 2rem and the maximum is 56px = 3.5rem. The maximum divided by the minimum is 1.75, which is under the 2.5 guideline, so zoom still works.
Exercise
A teammate sets body { font-size: 1.1vw; } so text grows on large screens. Name two problems with this and give a better approach.
Show answer
First, text becomes unreadably small on phones (at 360px, 1.1vw is about 4px) and ignores user font settings and zoom. Second, it grows without limit on very wide screens. A better approach is to keep body text at 1rem, limit the line length with max-width: 65ch, and use clamp() with a rem term only for headings and large spacing.