clamp()
Keeps a value between a minimum and a maximum, and lets it grow smoothly in between. The basis of fluid typography.
Example
#.hero {
padding: clamp(16px, 4vw, 40px);
}
.hero h2 {
font-size: clamp(1.5rem, 1rem + 3vw, 2.75rem);
}<section class="hero" style="border-radius: 14px; background: linear-gradient(135deg, #1e1b4b, #0c4a6e);">
<h2 style="margin: 0 0 6px; line-height: 1.15;">Learn CSS in 30 days</h2>
<p style="margin: 0; color: #cbd5e1;">The heading and spacing grow with the window width.</p>
</section>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
#clamp(<minimum>, <preferred value>, <maximum>)Parameters
#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.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.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
.demo { width: 140px; }
.demo h3 {
font-size: clamp(16px, 11cqi, 26px);
}preferred value
.demo { width: 200px; }
.demo h3 {
font-size: clamp(16px, 11cqi, 26px);
}maximum
.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
remcomponent tovw, for example1rem + 2vw.vwalone doesn’t respond to page zoom, which makes reading harder for people with low vision. - In components use
cqiinstead ofvw. The size then depends on the space the component was given, not on the whole window.
Related entries
#- min()Picks the smallest of the given values. It works as an upper limit, for example a width of 100% of the parent, but no more than 600px.
- max()Picks the largest of the given values. It works as a lower limit, for example spacing of 5% of the width, but never less than 16px.
- calc()Computes a value from a mathematical expression and lets you combine different units, for example percentages with pixels.
- font-sizeSets the font size.
- UnitsAn overview of length units in CSS: from fixed pixels, through font-relative em and rem, to viewport, container, and grid units.
- @containerApplies styles based on the size of a container instead of the whole window. A component adapts to the space it gets in the layout.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.