Sub topic 11
Scaling spacing across viewports
This chapter assumes you know the box model, rem and em units, CSS custom properties, and the spacing scale from the earlier chapters. A fixed spacing scale works well on one screen size. On a phone, 4rem between sections wastes space. On a wide monitor, 1rem can make groups look cramped. This chapter shows how to make spacing adapt while keeping the grouping ratios intact.
Why fixed spacing fails
Grouping depends on ratios. Space inside a group must be smaller than space between groups. If you shrink every value by the same factor, the ratios survive. If you shrink only some values, or let some stay fixed, groups can merge or split by accident.
Consider a card list with 1.5rem padding and a 1.5rem gap. On a wide screen this looks fine. On a 320px phone, the padding takes a large share of each card's width, and the text column gets narrow. The usual fix is to reduce the large values on small screens and leave the small values alone.
The common mistakes are:
- Scaling only section spacing and leaving card gaps fixed, so the relationship between levels shifts.
- Scaling every token by the same viewport-based formula, so small gaps become too small to see.
- Using
vwalone for a size, which ignores the user's font-size setting.
Three ways to adapt spacing
There are three approaches. Each suits a different kind of value.
| Approach | How it works | Best for | Main risk |
|---|---|---|---|
| Fluid values with `clamp()` | One formula grows with viewport width between a minimum and a maximum | Section padding, page margins, large gaps | Can ignore user zoom if written with `vw` only |
| Media queries on tokens | Redefine custom properties at breakpoints | Switching several tokens together | Jumps at breakpoints, more rules to maintain |
| Container queries | Change spacing based on the width of the component's container | Cards and widgets reused in different places | Needs a container to be declared |
You can combine them. A common pattern is fluid values for large spacing, fixed values for small spacing, and container queries for components.
Fluid spacing with clamp()
clamp(min, preferred, max) returns the preferred value, but never below the minimum or above the maximum. For spacing, the preferred value mixes a rem part with a vw part:
:root {
--space-section: clamp(2rem, 1rem + 4vw, 5rem);
}
.section {
padding-block: var(--space-section);
}Here is how this behaves. At a 320px viewport, 4vw is 12.8px, and 1rem + 12.8px is 28.8px. That is below the 2rem (32px) minimum, so the result is 32px. At 1000px, 4vw is 40px, so the preferred value is 56px (3.5rem). At 1400px, the preferred value is 72px, which is above the 5rem (80px) cap only at around 1600px. Past that width the value stays at 80px.
The 1rem in the formula matters. A pure 4vw value does not respond to the user's browser font-size setting. Adding a rem term keeps part of the value tied to text size. This helps users who zoom or enlarge text, because the spacing grows with the text.
Choosing the numbers
Pick the minimum and maximum first, based on what looks right at the smallest and largest viewports you support. Then derive the middle term. If you want the value to move from min at width w1 to max at width w2, the slope in vw is (max - min) / (w2 - w1) * 100. The intercept in rem is what remains. With a 2rem to 5rem range between 320px and 1280px, and 16px per rem, the range is 48px over 960px, which is a slope of 5vw. The intercept is 32px - 0.05 * 320px = 16px, or 1rem. That gives clamp(2rem, 1rem + 5vw, 5rem).
You do not have to compute this by hand every time. Build a few fluid tokens and reuse them.
Fluid tokens for the whole scale
You can make larger tokens fluid and keep small tokens fixed. This keeps the ratios sensible: the smallest gaps (label to input) are the same everywhere, and the largest gaps (section to section) grow.
:root {
--space-1: 0.25rem;
--space-2: 0.5rem;
--space-3: 1rem;
--space-4: clamp(1rem, 0.75rem + 1.2vw, 1.5rem);
--space-5: clamp(1.5rem, 1rem + 2vw, 2.5rem);
--space-6: clamp(2rem, 1rem + 4vw, 4rem);
}The first three tokens stay fixed. The last three scale. At small widths, --space-4 is 1rem, which equals --space-3. That is acceptable only if the two are never used for different levels of the same hierarchy. If a card list uses --space-3 inside cards and --space-4 between cards, the two levels would be equal on a phone, and the cards would lose their separation. In that case, check the smallest viewport and raise the minimum of --space-4 above --space-3, for example clamp(1.25rem, ...).
Media queries on tokens
Sometimes you want a deliberate change at a breakpoint. Redefine the custom properties, and every rule that uses them updates:
:root {
--page-margin: 1rem;
--card-padding: 1rem;
}
@media (min-width: 48em) {
:root {
--page-margin: 2rem;
--card-padding: 1.5rem;
}
}
@media (min-width: 72em) {
:root {
--page-margin: 4rem;
--card-padding: 2rem;
}
}
.page {
padding-inline: var(--page-margin);
}
.card {
padding: var(--card-padding);
}Using em in media queries ties the breakpoint to the user's font size, so a user with larger text gets the wider layout rules sooner. The trade-off against clamp() is that values jump at each breakpoint instead of changing smoothly. This is often fine for a few decisive changes, such as a single-column layout turning into two columns.
Container queries for components
A card placed in a narrow sidebar needs less padding than the same card in a wide main column, even when the viewport is the same. Media queries cannot tell the difference. Container queries can.
.card-wrapper {
container-type: inline-size;
}
.card {
padding: var(--space-3);
}
@container (min-width: 28rem) {
.card {
padding: var(--space-4);
}
}The wrapper declares itself as a container. The .card inside responds to the wrapper's width. You cannot style the container element itself with its own container query, which is why the wrapper and the card are separate elements.
Keeping grouping ratios across sizes
A useful check is to write down the ratios you intend and test them at three widths: narrow phone (about 320px), tablet (about 768px), and wide desktop (about 1280px).
A typical hierarchy for a form page:
| Level | Token | Intended relation |
|---|---|---|
| Label to input | `--space-1` | Smallest |
| Field to field | `--space-3` | About 3 to 4 times label gap |
| Group to group | `--space-5` | About 2 times field gap or more |
At each width, the order must stay the same: label gap smaller than field gap, field gap smaller than group gap. The absolute values can change. The order and a visible step between levels cannot.
Responsive gaps in grids
The gap property accepts the same fluid values. The auto-fill grid from earlier chapters needs no media query for its column count, and its gap can scale too:
.card-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(min(16rem, 100%), 1fr));
gap: var(--space-4);
}The min(16rem, 100%) guard prevents a card from overflowing a container narrower than 16rem. Without it, a minmax(16rem, 1fr) track can force horizontal scrolling on very small screens or in narrow containers.
When columns drop from three to one as the viewport narrows, the gap between rows becomes the main separator between cards. Check that it is still larger than the padding inside each card. If the gap is 1rem and the padding is 1.5rem, the content of one card sits closer to the next card's content than to its own edge, and the boundary becomes unclear.
Common errors
- Using
vwalone.padding: 5vwignores text size settings and can reach extreme values on very wide or very narrow windows. Wrap it inclamp()and add aremterm. - Forgetting the maximum. Without an upper bound, spacing keeps growing on large monitors and pushes content apart until groups drift.
- Scaling everything. Small gaps that scale with the viewport look inconsistent and can fall below a visible size.
- Testing only at your own screen size. Resize the window slowly from 320px upward. Watch for the point where two levels look the same.
- Mixing breakpoint jumps and fluid values in one hierarchy. A section that changes smoothly next to a card that jumps at 48em creates uneven ratios in between. Choose one approach per hierarchy where you can.
A quick check procedure
- Open the page in the browser's responsive mode.
- Set the width to 320px, 768px, and 1280px in turn.
- At each width, blur your eyes or apply a blur filter and look for the groups.
- Confirm that the same groups appear at each width.
- Where two groups merge or one splits, find the tokens involved and adjust their minimum or maximum.
Exercise
A page uses --space-3: 1rem inside cards and --space-4: clamp(1rem, 0.75rem + 1.2vw, 1.5rem) between cards. At 320px wide the cards look like one block. What is the cause, and how do you fix it without removing the fluid behavior?
Show answer
At 320px, 0.75rem + 1.2vw is 12px + 3.84px, which is 15.84px. That is below the 1rem minimum, so --space-4 resolves to 1rem, the same as --space-3. The space between cards equals the space inside them, so the grouping disappears. Raise the minimum of --space-4 above --space-3, for example clamp(1.5rem, 1rem + 1vw, 2rem), or lower the card padding on small screens. Then recheck at 320px that the gap between cards is clearly larger than the padding.
Exercise
You want section padding to go from 2rem at a 400px viewport to 6rem at a 1200px viewport, with 16px per rem. Write the clamp() expression.
Show answer
The range is 64px (4rem) over 800px, so the slope is 64 / 800 * 100 = 8vw. At 400px, 8vw is 32px, which is already 2rem, so the intercept is 0. The expression is clamp(2rem, 8vw, 6rem). To keep part of the value tied to text size, you can shift some of it into a rem term, for example clamp(2rem, 1rem + 4vw, 6rem) is a close variant. That variant reaches 2rem at 400px (16px + 16px = 32px) and 4.5rem at 1200px, so it needs a larger slope to reach 6rem. Using 8vw directly is the exact answer.