Skip to content
Tutorials Center
My learning

Topics

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.

CSS
h1 {
  font-size: clamp(1.75rem, 1rem + 3vw, 3.5rem);
}

Try it

Fluid heading

Syntax

CSS
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 rem with vw (for example 1rem + 1vw) instead of pure vw. 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) or gap: 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 cqi units in the preferred value to scale with a container rather than the viewport.
  • Alternative: max-width and min-width constraints do the same for widths with simpler syntax.

Browser support

Supported in all current major browsers.