Sub topic 10
Fluid spacing and padding
This chapter assumes you know the min(), max(), and clamp() syntax from earlier chapters. It also assumes you know common length units (rem, vw, %) and the box model. Here you apply the functions to the space around and between elements.
Why spacing should be fluid
Fixed padding is a common source of layout problems. A section with padding: 4rem looks balanced on a desktop. On a 320px phone, that padding uses a large share of the width and squeezes the content.
The usual fix is a media query that lowers the padding at a breakpoint. With clamp(), one declaration can scale the padding smoothly instead. It has no breakpoint and no jump.
Scaling padding with clamp()
Use clamp(MIN, PREFERRED, MAX). The preferred value is usually a viewport unit such as vw, which grows with the screen. The minimum and maximum keep it in a sensible range.
section {
padding-block: clamp(1rem, 4vw, 3rem);
}This is how the browser resolves it:
- On a 320px viewport,
4vwis 12.8px. That is below1rem(16px), so the padding is 16px. - On a 800px viewport,
4vwis 32px. This falls between the limits, so the padding is 32px. - On a 1600px viewport,
4vwis 64px. That is above3rem(48px), so the padding is 48px.
Setting a floor with max()
Sometimes you only need a lower bound. max() returns the larger of its arguments, so it guarantees a minimum.
button {
padding-inline: max(1rem, 2vw);
}The button never has less than 1rem of horizontal padding. On wide screens, 2vw takes over and the padding grows. There is no upper limit here, so on very wide screens the padding keeps growing. Use clamp() if you need a ceiling.
Fluid gaps and margins
Padding is not the only place this works. Any property that accepts a length can take these functions, including gap, margin, and inset.
.stack {
display: grid;
gap: clamp(0.5rem, 2vw, 1.5rem);
}
article {
margin-block: clamp(1rem, 3vw, 2.5rem);
}Percentages in padding
Percentage padding is relative to the width of the containing block. This is true for vertical padding too. That behavior can be useful with min(), because it lets you cap a percentage.
.panel {
padding: min(5%, 2rem);
}The padding is 5% of the container's width, but never more than 2rem. Note that in a very wide container, 5% would otherwise become large. The cap prevents that.
Common mistakes
- Using only
vwfor the preferred value. Padding is less of a concern than text, but a mix such as0.5rem + 2vwstill respects the user's root font size. - Forgetting that
clamp()needs the minimum to be at most the maximum. If the minimum is larger than the maximum, the minimum wins. - Writing a negative or unitless number.
clamp(1rem, 4, 3rem)is invalid. Every argument must be a length, percentage, or valid math expression.
Exercise
A card should have horizontal padding of at least 1rem, at most 2.5rem, and grow with the viewport in between at 3vw. Write the declaration. What padding results at a 400px viewport?
Show answer
padding-inline: clamp(1rem, 3vw, 2.5rem); At 400px, 3vw is 12px. That is below the 1rem minimum (16px), so the padding is 16px.