The CSS clamp calculator formula
A fluid clamp value can be built from two points: a minimum size at a minimum viewport width, and a maximum size at a maximum viewport width. The calculator finds the straight line that connects those points. It converts the line's slope into a viewport-relative vw coefficient and calculates the intercept.
vw coefficient = (max size − min size)
÷ (max viewport − min viewport)
× 100
intercept = min size − (vw coefficient ÷ 100
× min viewport)
preferred = intercept + vw coefficient × 1vwWhen the size units are rem, the endpoints are first converted to pixels using the assumed root font size, then the intercept is converted back to rem for the output. This is why the root font-size assumption is part of a rem calculator.
Worked example: 1rem to 3rem
Suppose the minimum size is 1rem and the maximum is 3rem, from a 360px viewport to a 1440px viewport. Assuming 1rem equals 16px, the pixel endpoints are 16px and 48px.
- Size change: 48 − 16 = 32px.
- Viewport change: 1440 − 360 = 1080px.
- VW coefficient: 32 ÷ 1080 × 100 ≈ 2.963.
- Intercept: 16 − (2.963 ÷ 100 × 360) ≈ 5.333px, or 0.3333rem.
font-size: clamp(1rem, calc(0.3333rem + 2.963vw), 3rem);
The browser is not required to display a rounded intermediate value exactly as the calculator text does. The formula's full-precision values and final bounds determine the result; a practical generator rounds the emitted CSS to a chosen number of decimal places.
Check the endpoints, not just the code
At the minimum viewport, the preferred expression should equal the minimum size. At the maximum viewport, it should equal the maximum size. Between those widths, the preferred value should move linearly. Outside the range, clamp() holds the value at the minimum or maximum.
A preview slider makes these points easy to inspect. It is not enough to generate syntactically valid CSS; test the layout where the value changes fastest and where content is close to wrapping.
Why rem and px output differ
CSS px is a CSS reference pixel; rem is relative to the root element's font size. If you choose rem output, the generator must use a root font-size assumption to map the viewport endpoints to the rem line. The output will then scale with the actual root font size in the browser. If the page's root font size is different from the assumption, the exact match at the selected viewport endpoints will differ from the calculator's sample.
That is not a reason to avoid rem. It's a reason to state the assumption and test under realistic browser settings. For fluid type, relative units can make the interface more responsive to the reader's font-size preference.
Common calculator mistakes
- Max size is less than or equal to min size: the normal increasing fluid scale has no positive range to interpolate.
- Viewport max is not greater than viewport min: slope calculation divides by a zero or negative interval.
- Wrong units: subtracting a rem value directly from a px value without conversion creates incorrect math.
- Too many decimals: copy becomes noisy and hard to maintain; precision should be sufficient for the design, not arbitrary.
- Ignoring real content: a fluid value can still cause awkward wrapping or spacing when the text, language or component changes.
Use the CSS clamp calculator
Set the values, review the generated declaration and move the preview slider through the intended viewport range. If the result looks abrupt at a real breakpoint, consider changing the endpoints or combining clamp with a breakpoint rather than forcing a single function to handle a structural layout change.
Open the live CSS clamp calculator to calculate your values, or review the worked CSS clamp examples for typography and layout properties.
References and further reading
Primary standards and documentation used to support this guide.
Frequently asked questions
How do I calculate a clamp value by hand?
Convert the size endpoints to one unit, calculate (max size minus min size) divided by (max viewport minus min viewport) and multiply by 100 to get the vw coefficient. Then calculate the intercept from the minimum size and minimum viewport.
Can the intercept be negative?
Yes. A negative intercept can be mathematically correct for some endpoint combinations. It should still be tested in the intended CSS property and viewport range.
Why does the generated value use calc()?
calc() combines a fixed intercept with a viewport-relative term. clamp() then limits the computed preferred value to the minimum and maximum.
How many decimals should a calculator output?
Enough to make the interpolation accurate for the design. Three or four decimal places are often readable; test the rounded output at both endpoints and relevant in-between widths.
Put the explanation to work.
Generate the value with live preview, then test it in the context of your actual design.