clamp()

Keeps a value between a minimum and a maximum, and lets it grow smoothly in between. The basis of fluid typography.

On this page

Example

#
CSS
.hero {
  padding: clamp(16px, 4vw, 40px);
}

.hero h2 {
  font-size: clamp(1.5rem, 1rem + 3vw, 2.75rem);
}
Preview

The heading grows with the preview frame, but will never be smaller than 24 px (1.5rem) or larger than 44 px (2.75rem). The spacing around the content works the same way, between 16 and 40 px.

Definition and usage

#

The clamp(MIN, PREFERRED, MAX) function returns the preferred value, but never less than the minimum and never more than the maximum. The expression clamp(1rem, 4vw, 2rem) reads: “4% of the window width, but not less than 1rem and not more than 2rem”. It is shorthand for max(MIN, min(PREFERRED, MAX)), only shorter and clearer.

The most common use is fluid typography: a heading grows with the window, but doesn’t become microscopic on a phone or gigantic on a wide monitor. It works just as well for spacing (padding: clamp(12px, 3vw, 32px)) and widths (width: clamp(240px, 50%, 480px)). One declaration then replaces several breakpoints in media queries.

The middle value should depend on something variable: vw, cqi, or percentages. A constant, such as clamp(1rem, 20px, 2rem), never changes. For font size, add a rem component to vw, for example clamp(1rem, 0.8rem + 1.5vw, 2rem). The text then also responds to page zoom and the browser’s font settings.

If the minimum is greater than the maximum, the minimum wins. In all three arguments you can write expressions without the word calc and mix units.

Syntax

#
Syntax
clamp(<minimum>, <preferred value>, <maximum>)

Parameters

#
  • minimum

    16px

    The lower bound. The container is 140 px wide here, so 11cqi gives about 15 px, which is too small, and the text gets the minimum: 16 px. Drag the frame’s bottom-right corner to the right and you will see when the text starts to grow.
  • preferred value

    11cqi

    The value that applies as long as it stays within the bounds. With a 200 px wide container, 11cqi gives 22 px, somewhere between the minimum and the maximum. The text grows and shrinks with the frame.
  • maximum

    26px

    The upper bound. The container fills the whole frame, so 11cqi exceeds 26 px and the text stops at the maximum. Without it the heading would grow endlessly in a wide container.

Parameters in practice

#

minimum

The lower bound. The container is 140 px wide here, so 11cqi gives about 15 px, which is too small, and the text gets the minimum: 16 px. Drag the frame’s bottom-right corner to the right and you will see when the text starts to grow.
.demo { width: 140px; }
.demo h3 {
  font-size: clamp(16px, 11cqi, 26px);
}

preferred value

The value that applies as long as it stays within the bounds. With a 200 px wide container, 11cqi gives 22 px, somewhere between the minimum and the maximum. The text grows and shrinks with the frame.
.demo { width: 200px; }
.demo h3 {
  font-size: clamp(16px, 11cqi, 26px);
}

maximum

The upper bound. The container fills the whole frame, so 11cqi exceeds 26 px and the text stops at the maximum. Without it the heading would grow endlessly in a wide container.
.demo { width: 100%; }
.demo h3 {
  font-size: clamp(16px, 11cqi, 26px);
}

Browser support

#

Widely available · since 2020

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • The order of arguments is fixed: the minimum first, then the preferred value, and the maximum last.
  • For font size, add a rem component to vw, for example 1rem + 2vw. vw alone doesn’t respond to page zoom, which makes reading harder for people with low vision.
  • In components use cqi instead of vw. The size then depends on the space the component was given, not on the whole window.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.