CSS Reference ยท Functions
The clamp() function
Reference for the CSS clamp() function: minimum, preferred and maximum values, fluid typography, accessibility with rem, with live examples and usage notes.
clamp() takes three values: a minimum, a preferred value and a maximum. It returns the preferred value, unless that is smaller than the minimum (then the minimum) or larger than the maximum (then the maximum). It is the standard tool for fluid typography and fluid spacing.
h1 { font-size: clamp(1.75rem, 1rem + 3vw, 3.5rem); }
Try it
Syntax
clamp( <min>, <preferred>, <max> ) /* equivalent to max(min, min(preferred, max)) */
Syntax and arguments
| Argument | Description |
|---|---|
<min> |
The lowest value the result can have. |
<preferred> |
The ideal value, often containing a viewport or container unit. |
<max> |
The highest value the result can have. |
Each argument may be a calculation such as 1rem + 2vw. They must have compatible types. The specification also allows none for the minimum or maximum, but support is not universal, so write real values for portable CSS.
Usage notes
- If min is larger than max, the minimum wins (the result equals the minimum).
- Accessible fluid type: mix
remwithvw(for example1rem + 1vw) instead of purevw. A purely viewport-based size ignores the user's text-size preference and can fail zoom requirements. - Spacing and widths: it works for any numeric property, such as
width: clamp(16rem, 50%, 40rem)orgap: clamp(8px, 2vw, 24px). - Predictable ends: pick the min and max first for the smallest and largest screens you design for, then solve for the middle.
- Container-based variants: use
cqiunits in the preferred value to scale with a container rather than the viewport. - Alternative:
max-widthandmin-widthconstraints do the same for widths with simpler syntax.
Browser support
Supported in all current major browsers.
Related
min(),max(),calc()font-size- Lessons: Responsive typography, Math functions