CSS Clamp

Gap clamp() generator

Fluid grid and flexbox gaps that grow with the screen.

gap
Screen width
1152px

Actions

Clamp sizes

The smallest and largest size, for padding, margin or font size.

rem
rem

Viewport settings

The screen widths where the value starts and stops growing.

rem
rem

The clamped value will be between 0.75rem and 2rem, applied linearly between viewports of 24rem and 120rem.

A fluid gap for grid and flexbox

Gutters that look right on a desktop waste space on a phone, and gutters sized for a phone make a desktop layout feel cramped. The preview applies the clamp() value as the gap of a three-column grid, so you can see the spacing change as you drag the screen width.

From 12px to 32px between 384px and 1920px, the value is clamp(0.75rem, 0.438rem + 1.302vw, 2rem). Use it as gap, row-gap or column-gap on any grid or flex container.

Tips

  • Keep gaps smaller than the page gutter, so groups read as groups.
  • Store the value in a custom property like --layout-gap and reuse it for grids, stacks and flex rows.
  • Inside components that move between a sidebar and a main column, swap vw for cqi. See the container query units guide.

See it in a layout

The dashboard example combines a fluid gap with fluid page gutters and card padding. For padding and margin, use the spacing generator.

Frequently asked questions

Does gap work with clamp()?

Yes. gap, row-gap and column-gap accept any length, including clamp(). It works for CSS grid and for flexbox in every current browser.

Should gap use vw or percentages?

Use a clamp() with a vw part. Percentages in gap resolve against the container and behave differently for rows and columns, while a clamp() gives you a predictable minimum and maximum in rem.

Can rows and columns have different fluid gaps?

Yes. Set row-gap and column-gap separately, or write gap with two values: gap: clamp(1rem, 0.5rem + 2vw, 2rem) clamp(0.75rem, 0.438rem + 1.302vw, 2rem). The first value is the row gap.

How do gap and padding relate?

Use the same fluid token, or a fixed ratio between them, for the page gutter, the grid gap and the card padding. The layout then keeps the same rhythm at every width. The dashboard example shows this with three tokens.