Sub topic 10
Fluid spacing and padding
This chapter assumes you know the clamp(), min() and max() syntax from the earlier chapters, and that you are comfortable with rem, vw and %. Here you apply those functions to spacing: padding, margins and gaps that grow and shrink with the viewport and never leave a range you chose.
Why spacing needs to be fluid
Fixed spacing is a common source of layout bugs. A padding: 3rem section looks balanced on a desktop. On a 320px phone it uses 96px of the width for padding alone, and the content gets cramped. The usual fix is a media query that lowers the padding at some breakpoint. That fix has two weaknesses:
- Spacing jumps at the breakpoint instead of changing smoothly.
- Every component that needs this behavior repeats the media query.
A comparison function replaces the jump with a continuous curve. One declaration states the smallest acceptable spacing, how spacing should track the viewport, and the largest acceptable spacing.
Fluid padding with clamp()
Vertical section padding is the standard example:
section {
padding-block: clamp(1rem, 4vw, 3rem);
}The browser evaluates the three arguments and picks the middle one after applying the bounds:
1remis the floor.4vwis the preferred value, 4% of the viewport width.3remis the ceiling.
With a 16px root font size, the preferred value only matters between 400px and 1200px. The table shows the result at several viewport widths.
| Viewport width | 4vw | Computed padding |
|---|---|---|
| 320px | 12.8px | 16px (floor) |
| 600px | 24px | 24px |
| 1000px | 40px | 40px |
| 1400px | 56px | 48px (ceiling) |
The chart has three phases: flat, a ramp, then flat again. The ramp is the only region where the 4vw term controls the result.
Using max() for a floor
max() suits spacing that must never get too small, while growing freely on large screens:
button {
padding-inline: max(1rem, 2vw);
}The padding is at least 1rem. Once 2vw exceeds 1rem, which happens above a 800px viewport at a 16px root, the padding follows the viewport. Because max() has no upper bound, use it for things like touch targets, where a minimum matters more than a cap.
A practical use of max() is respecting device safe areas, such as phone notches. The env() function returns the inset, and max() keeps your normal gutter when the inset is zero:
.page {
padding-inline: max(1rem, env(safe-area-inset-left));
}For the inset to be nonzero on notched devices, the page also needs viewport-fit=cover in its viewport meta tag. Without it, the inset is 0 and you simply get 1rem.
Gaps and margins
Padding is not the only spacing property that accepts these functions. gap, margin, row-gap and column-gap all take lengths and percentages, so they take comparison functions too.
.stack {
display: grid;
gap: clamp(0.5rem, 0.25rem + 1.5vw, 1.5rem);
}
.card {
padding: clamp(1rem, 3vw, 2rem);
margin-block-end: clamp(1rem, 2vw + 0.5rem, 2.5rem);
}Two things to notice. First, the preferred value can mix units (0.25rem + 1.5vw). Inside a comparison function you can write + and - directly, without calc(). Second, you can give each property its own curve. Gaps usually should change less than section padding, so the card padding above has a narrower range than the section padding earlier.
Choosing units for the preferred value
The preferred value decides how spacing behaves between the bounds. Your options:
| Preferred value | Behavior | Typical use |
|---|---|---|
| `vw` only, such as `4vw` | Proportional to viewport, zero at zero width | Quick spacing where the floor handles small screens |
| `rem + vw`, such as `0.5rem + 2vw` | Has a base amount plus growth | Most spacing, because the base respects user font settings |
| `%` | Relative to the containing block's width | Spacing that should follow a component, not the viewport |
| `em` | Relative to the element's own font size | Spacing that should scale with fluid type |
Percentages in padding and margin
Percentage values in padding and margin resolve against the width of the containing block. This applies even to padding-block and margin-block, in a horizontal writing mode. So padding-block: clamp(1rem, 5%, 3rem) in a 600px-wide parent gives 30px, regardless of how tall the parent is. This is useful for component-based spacing, and surprising if you expect height-based percentages.
em for type-linked spacing
If an element's font size is fluid, padding in em follows it automatically:
.badge {
font-size: clamp(0.875rem, 0.8rem + 0.4vw, 1.125rem);
padding: 0.4em 0.8em;
}The badge keeps the same proportions at every size, and you write one clamp() instead of three. Avoid nesting such elements, though, because em compounds when the font size is inherited and then changed.
Putting it together
This page uses a container, fluid section padding, a fluid grid gap and a button floor. It runs as written.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fluid spacing</title>
<style>
.container {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}
section {
padding-block: clamp(1rem, 4vw, 3rem);
}
.cards {
display: grid;
gap: clamp(0.5rem, 0.25rem + 1.5vw, 1.5rem);
grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
}
.card {
padding: clamp(1rem, 3vw, 2rem);
background: #eef2f7;
}
button {
padding-block: 0.5rem;
padding-inline: max(1rem, 2vw);
}
</style>
</head>
<body>
<div class="container">
<section>
<div class="cards">
<div class="card">One</div>
<div class="card">Two</div>
<div class="card">Three</div>
</div>
<button>Continue</button>
</section>
</div>
</body>
</html>Resize the window and watch the section padding, card padding and gap change smoothly. There are no media queries.
Pitfalls specific to spacing
- Viewport-only preferred values.
clamp(1rem, 4vw, 3rem)ignores the user's font-size setting in the ramp region, and zooming the page only works indirectly. Adding aremterm, as in0.5rem + 3vw, keeps part of the spacing tied to the user's settings. The accessibility chapter covers this in depth. - Padding and width interplay. If an element has
width: 100%and fluidpadding-inline, the content box overflows unlessbox-sizing: border-boxis set. Most resets set it globally. - Negative results. Padding cannot be negative. A calculation such as
clamp(0px, 2vw - 1rem, 2rem)is safe because the floor is0. Without a floor, a negative result is clamped to zero for padding, but margin accepts negatives, so margin math can pull elements out of place. - Scrollbar width.
vwincludes the width of a classic scrollbar. On desktop, a100vw-based value can be slightly larger than the visible area. For spacing this is a minor difference, but it matters when you compute widths. - Too much range. A gap that grows from
0.5remto4remis hard to reason about. Pick ranges you can describe: "tight on phones, roomy on desktop".
Summary of patterns
- Section spacing:
clamp(min, preferred, max)with aremfloor and ceiling. - Minimum gutters or targets:
max(1rem, 2vw). - Device insets:
max(1rem, env(safe-area-inset-left)). - Spacing linked to type:
emunits on an element with fluidfont-size. - Component-relative spacing:
%insideclamp(), remembering it resolves against width.
Exercise
With a 16px root font size, what padding does padding-block: clamp(1rem, 4vw, 3rem) compute to at a 700px viewport width, and at a 1500px viewport width?
Show answer
At 700px, 4vw is 28px, which lies between 16px (1rem) and 48px (3rem), so the padding is 28px. At 1500px, 4vw is 60px, which exceeds the ceiling, so the padding is 48px.
Exercise
Write a declaration that gives an element left and right padding of at least 1.25rem, growing with 3vw on wide screens, but never more than 4rem.
Show answer
padding-inline: clamp(1.25rem, 3vw, 4rem); The floor is 1.25rem, the preferred value is 3vw, and the ceiling is 4rem. An equivalent form is min(max(1.25rem, 3vw), 4rem).
Exercise
A parent is 800px wide. What is the padding-block of a child with padding-block: clamp(1rem, 5%, 3rem) at a 16px root font size, and why does the parent's height not matter?
Show answer
5% of 800px is 40px, which is between 16px and 48px, so the padding is 40px. Percentage padding and margin resolve against the width of the containing block, even for block-direction properties, so the parent's height is not used.