The ** clamp()** CSS function clamps a value between an upper and lower bound.

`clamp()`

enables selecting a middle value within a range of values between a defined minimum and maximum. It takes three parameters: a minimum value, a preferred value, and a maximum allowed value. The `clamp()`

function can be used anywhere a `<length>`

, `<frequency>`

, `<angle>`

, `<time>`

, `<percentage>`

, `<number>`

, or `<integer>`

is allowed.`clamp(MIN, VAL, MAX)`

is resolved as `max`

(MIN, `min`

(VAL, MAX))

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Note that using `clamp()`

for font sizes, as in these examples, allows you to set a font-size that grows with the size of the viewport, but doesn't go below a minimum font-size or above a maximum font-size. It has the same effect as the code in Fluid Typography but in one line, and without the use of media queries.

## Syntax

The `clamp()`

function takes three comma separated expressions as its parameter, in the order of minimum value, preferred value, maximum value.

The minimum value is the smallest (most negative) value. This is the lower bound in the range of allowed values. If the preferred value is less than this value, the minimum value will be used.

The preferred value is the expression whose value will be used as long as the result is between the minimum and maximum values.

The maximum value is the largest (most positive) expression value to which the value of the property will be assigned if the preferred value is greater than this upper bound.

The expressions can be math functions (see `calc()`

for more information), literal values, or other expressions, such as `attr()`

, that evaluate to a valid argument type (like `<length>`

), or nested `min()`

and `max()`

functions. As math expressions, so you can use addition, subtraction, multiplication and division without using the `calc()`

function itself. You may also use parentheses to establish computation order when needed.

You can use different units for each value in your expressions, and different units in any math function making up any of the arguments.

### Notes

- Math expressions involving percentages for widths and heights on table columns, table column groups, table rows, table row groups, and table cells in both auto and fixed layout tables
*may*be treated as if`auto`

had been specified. - It is permitted to nest
`max()`

and`min()`

functions as expression values, in which case the inner ones are treated as simple parentheses. The expressions are full math expressions, so you can use direct addition, subtraction, multiplication and division without using the calc() function itself. - The expression can be values combining the addition ( + ), subtraction ( - ), multiplication ( * ) and division ( / ) operators, using standard operator precedence rules. Make sure to put a space on each side of the + and - operands. The operands in the expression may be any
`<length>`

syntax value. You can use different units for each value in your expression. You may also use parentheses to establish computation order when needed. - Oftentimes you will want to use
`min()`

and`max()`

within a`clamp()`

function.

### Formal syntax

clamp( <calc-sum>#{3} )where

`<calc-sum> = <calc-product> [ [ '+' | '-' ] <calc-product> ]*`

where

`<calc-product> = <calc-value> [ '*' <calc-value> | '/' <number> ]*`

where

`<calc-value> = <number> | <dimension> | <percentage> | ( <calc-sum> )`

## Accessibility concerns

TBD

## Specifications

Specification | Status | Comment |
---|---|---|

CSS Values and Units Module Level 4 The definition of 'clamp()' in that specification. |
Editor's Draft | Initial definition. |

## Browser compatibility

Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|

`<clamp()>` | Chrome Full support 79 | Edge Full support 79 | Firefox Full support 75 | IE No support No | Opera Full support 66 | Safari Full support 13.1 | WebView Android Full support 79 | Chrome Android Full support 79 | Firefox Android No support No | Opera Android No support No | Safari iOS Full support 13.4 | Samsung Internet Android No support No |

### Legend

- Full support
- Full support
- No support
- No support