What clamp() actually does
The CSS clamp() function takes three values: a minimum, a preferred, and a maximum. At any given viewport width, the browser evaluates the preferred value and returns it if it falls between the bounds. Otherwise it returns the nearest bound. The result is a single property declaration that scales smoothly across viewport widths without media queries or visual "jumps."
The preferred value is a linear formula in vw units. The formula is what this calculator generates — it solves the line between your two points (min-vw, min-size) and (max-vw, max-size), then outputs the slope and intercept in the form a + b·vw.
The formula, plainly
If your minimum size is S₁ at viewport width V₁, and your maximum size is S₂ at viewport width V₂:
- Slope = (S₂ − S₁) / (V₂ − V₁), expressed as a percentage and emitted as
vw. - Intercept = S₁ − slope · V₁, emitted in your chosen unit (
remorpx).
The full output is clamp(min, intercept + slope·vw, max). The calculator above does this math for you and formats the output for both rem and px.
When to use clamp()
Use it for things that should scale with the viewport but stay within sane limits:
- Headings (especially large display type)
- Body text that should grow slightly on desktop
- Section padding and hero spacing
- Container widths that should grow until a cap
- Gap and grid-row sizes
Don't use it for things that should be the same on every screen — icon sizes, border widths, fixed UI element heights. Fluidity is a feature, not a default.
Why rem instead of px?
The calculator defaults to rem because rem respects the user's browser font-size setting. A user who has bumped their default from 16px to 20px for accessibility reasons still gets proportionally larger text. Hard-coding px values everywhere is the most common accessibility regression in modern CSS.
If your codebase is all px and changing it is more work than it's worth, the calculator outputs px values too — just toggle the unit.
Common gotchas
- Min and max are not viewports — they're sizes. The first and third arguments of
clamp()are the minimum and maximum output values, not the viewport widths. - The middle value must not exceed the bounds at the corner viewports. The calculator handles this for you, but if you write
clamp()by hand, the middle expression evaluated at your min viewport should equal your min size, and at your max viewport should equal your max size. - vw vs cqi. Inside a container query, you may want to use
cqi(container inline-size) instead ofvw. The math is identical — replacevwwithcqi. - Accessibility check. Don't set a minimum size below 14–16px for body text. Users zooming in or with poor eyesight will struggle.