Sub topic 10
Responsive type and spacing
This chapter assumes you can write basic CSS, know px, rem, and vw units, and have read the chapters on fluid layouts and media queries. Here you will make type and spacing scale with the screen, so you need fewer breakpoints for them.
Why type and spacing need to respond
A heading that looks right in a 1440px desktop frame often wraps badly at 360px. Spacing has the same problem. A 4rem gap between sections feels generous on a desktop and wastes a third of a phone screen.
You could set a new size at every breakpoint. That works, but the size jumps at each breakpoint and stays fixed between them. A better approach lets values scale smoothly and uses breakpoints only for real layout changes.
Start with relative units
Set sizes in rem, not px. A rem is the root font size, which is 16px by default. If a user raises their browser's default text size, rem values grow with it. Pixel values do not.
body {
font-size: 1rem;
line-height: 1.5;
}Use a unitless line-height. It is multiplied by each element's own font size, so headings and body text both keep sensible spacing.
Fluid type with clamp()
clamp(min, preferred, max) returns the preferred value, but never below the minimum or above the maximum. Put a vw term in the preferred value and the size follows the viewport.
h1 {
font-size: clamp(1.75rem, 1.25rem + 2vw, 3rem);
}This heading is never smaller than 1.75rem (28px) or larger than 3rem (48px). Between those limits it grows with the viewport width.
Always mix vw with a rem term, as in 1.25rem + 2vw. A value made only of vw ignores the user's text-size setting, and it can become too small for people who zoom in.
Fluid spacing
Spacing can use the same technique. Store the values as custom properties so that every component shares one scale.
:root {
--space-s: clamp(0.5rem, 0.4rem + 0.5vw, 1rem);
--space-m: clamp(1rem, 0.8rem + 1vw, 2rem);
--space-l: clamp(2rem, 1.5rem + 2.5vw, 4rem);
}
.section {
padding-block: var(--space-l);
}
.card-list {
display: grid;
gap: var(--space-m);
}Name the tokens by role, not by value. Then you can retune the numbers later without renaming anything. This also helps your design tool: define the same tokens as variables there, so the frames and the CSS stay in sync.
Keep line length readable
Body text is easiest to read at roughly 45 to 75 characters per line. On a phone this happens on its own. On a wide desktop frame, text can stretch far past that. Limit the width with the ch unit, which is about the width of the character "0" in the current font.
.prose {
max-width: 65ch;
}Where breakpoints still help
Fluid values handle gradual change. Use a media query when the design should change in kind, such as moving from a stacked layout to columns. Type often needs no breakpoint of its own.
| Need | Tool |
|---|---|
| Heading grows smoothly with width | `clamp()` |
| Gap between items scales | `clamp()` in a spacing token |
| Text line stays readable | `max-width` in `ch` |
| Layout changes from one column to two | Media query |
Check it in the design frames
In your mobile, tablet, and desktop frames, record the minimum and maximum size for each text style. For example, the h1 is 28px in the mobile frame and 48px in the desktop frame. Those two numbers become the first and last arguments of clamp(). The tablet frame then shows a value between them, and you can check it against the live page.
Exercise
A designer specifies body headings of 24px on mobile and 40px on desktop, with the desktop size reached at a 1280px viewport. Why is font-size: 4vw a poor way to do this, and what would you write instead?
Show answer
A value made only of vw ignores the user's text-size settings and does not stop at a minimum or maximum, so it can be too small on phones and too large on wide screens. Use clamp() with rem limits and a mixed preferred value, for example font-size: clamp(1.5rem, 1rem + 1.6vw, 2.5rem);. This gives 24px at the minimum and 40px at the maximum.