Home / CSS guides / Guide: CSS clamp()

CSS clamp(): A Practical Guide

clamp() lets one declaration scale between a minimum and a maximum. Here is the syntax, working examples, and what to avoid.

Syntax

clamp() takes three values: a minimum, a preferred value, and a maximum. It returns the preferred value unless that falls below the minimum or above the maximum.

font-size: clamp(1rem, 0.9583rem + 0.2083vw, 1.125rem);

It is shorthand for max(min, min(preferred, max)).

Examples

padding: clamp(1rem, 4vw, 3rem);
width: clamp(20rem, 90%, 60rem);
gap: clamp(0.75rem, 2vw, 1.5rem);

Common mistakes

Using only vw as the preferred value breaks browser zoom. Putting the minimum above the maximum makes the minimum always win. Wrapping the arithmetic in calc() is unnecessary: clamp() accepts expressions like 1rem + 2vw directly.

Browser support

clamp() works in all current major browsers.

Fluid layout examples

h1 { font-size: clamp(2rem, 1.3333rem + 3.3333vw, 4rem); }
.container { padding-inline: clamp(1rem, 5vw, 4rem); }
section { margin-block: clamp(2rem, 6vw, 5rem); }

clamp() compared with min() and max()

min() sets an upper limit and max() sets a lower limit. clamp() does both in one call, so it is the right choice when you need a floor and a ceiling.

Next steps

Generate exact values with the clamp calculator, convert pixel sizes with the rem converter, or build a full type scale with the fluid typography calculator.

Skip the maths. Enter your sizes and viewport widths, and get the clamp() value instantly.

Open the clamp() calculator

Frequently asked questions

Do I need calc() inside clamp()?

No. Math expressions work directly inside clamp().

Can I use clamp() for things other than font size?

Yes. It works for padding, margin, gap, width, height and any length value.

How do I get the preferred value?

Use the calculator linked above. It computes the rem and vw parts from your sizes and viewport widths.

Can I nest clamp() inside other functions?

Yes. clamp() can be used inside calc(), and other math functions can be used inside clamp().

Does clamp() work in media queries?

No. It is a value function for properties, not a condition. Use it to reduce the number of media queries you need.