Sub topic 11
Scaling spacing across viewports
This chapter assumes you know the spacing scale from earlier in the tutorial, the box model, and rem and vw units. It shows how to make that scale adapt to the screen without losing the groupings it creates.
Why fixed spacing breaks
A 4rem gap between page sections looks right on a 1440px desktop. On a 360px phone it uses a large share of the screen, and the content is pushed far apart. The reverse also happens. A 1rem gap that suits a phone looks cramped on a wide monitor, so sections stop reading as separate groups.
The aim is not to make every gap larger or smaller. The aim is to keep the relationships between gaps. Gaps inside a group must stay smaller than gaps between groups at every width.
Fluid spacing with clamp()
clamp(min, preferred, max) returns the preferred value, but never goes below the minimum or above the maximum. If the preferred value mixes a rem offset with a vw term, the result grows with the viewport:
:root {
--space-section: clamp(2rem, 1rem + 4vw, 5rem);
}
.section {
padding-block: var(--space-section);
}At a 360px viewport, 1rem + 4vw is about 30px. That is below the 2rem minimum (32px), so the minimum applies. At 1440px it is about 73px, which is inside the range. Above roughly 1800px the 5rem maximum applies. Adding 1rem to the vw term keeps the value responding to the user's font size as well as the viewport.
Scale the tokens, not the components
Define fluid values once, as custom properties. Components keep using the tokens and need no changes:
:root {
--space-3: 1rem;
--space-4: clamp(1rem, 0.75rem + 1vw, 1.5rem);
--space-5: clamp(1.5rem, 1rem + 2vw, 2.5rem);
--space-6: clamp(2rem, 1rem + 4vw, 4rem);
}
.card-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: var(--space-4);
}Keep the small tokens fixed. Gaps such as label-to-input work at any width, and making them fluid adds little. Make the larger tokens fluid, because they cause the layout problems.
Check that the order of your tokens holds at both ends of the range. --space-4 must stay smaller than --space-5 at every width. If a fluid token's maximum is below the next token's minimum, the scale stays in order. If the ranges overlap, two levels of grouping can end up the same size.
Changing tokens with a media query
You can also redefine tokens at a breakpoint. This suits cases where you want a distinct change, such as a layout going from one column to two:
:root {
--space-gutter: 1rem;
}
@media (min-width: 48rem) {
:root {
--space-gutter: 2rem;
}
}
.page {
padding-inline: var(--space-gutter);
}Use clamp() for smooth scaling and media queries for abrupt changes. Using both on the same token makes the result hard to predict.
Common errors
- Using
vwalone.padding: 4vwignores the user's font size and has no minimum or maximum. Always wrap it inclamp()withrembounds. - Making every gap fluid. Gaps inside components then drift, and the ratios between levels stop being predictable.
- Letting the ratios collapse. On small screens, group gaps and field gaps can shrink to almost the same size. Check the layout at narrow widths.
- Forgetting the container. Viewport units respond to the window, not to a component's own width. A card in a narrow sidebar on a wide screen still gets large spacing.
Exercise
A token is defined as --space-5: clamp(1.5rem, 1rem + 2vw, 2.5rem);. On a 400px-wide viewport with a 16px root font size, what value does it resolve to, and why?
Show answer
The preferred value is 1rem + 2vw = 16px + 8px = 24px. The minimum is 1.5rem = 24px, so the result is 24px (1.5rem). The preferred value equals the minimum here, so clamp() returns it unchanged. Below 400px the minimum would take over.