Math
Functions that compute a number, a dimension such as a length, angle or time, or a percentage. They can be nested inside each other, can mix compatible units, and work anywhere a value of their result type is allowed. The value types themselves are under Types; functions that build other values from references, such as var() and attr(), are under Values & references.
Arithmetic & comparison
calc()
Evaluates an arithmetic expression with +, -, * and /, and can mix units, such as a percentage minus a fixed length. + and - need whitespace on both sides; * and / do not.
| Syntax | calc( <calc-sum> ) |
|---|---|
| Returns | A number, dimension or percentage: whatever type the expression resolves to |
| Support | Chrome 26 · Firefox 16 · Safari 7 |
| Spec | css-values-4 |
A <calc-sum> is the expression itself: values, nested math functions and brackets joined by the four operators. Inside any math function you can also use these constant keywords:
| Constant | Value | Support |
|---|---|---|
e | The base of the natural logarithm, about 2.718 | Chrome 110 · Firefox 108 · Safari 15.4 |
pi | The ratio of a circle's circumference to its diameter, about 3.142 | Chrome 109 · Firefox 108 · Safari 15.4 |
infinity, -infinity | The largest and smallest possible values; the result is clamped to the range the property allows | Chrome 99 · Firefox 114 · Safari 16 |
NaN | Not a number; a top-level NaN is treated as zero | Chrome 99 · Firefox 114 · Safari 16 |
width: calc(100% - 2rem);
height: calc(100dvh - var(--header));
z-index: calc(infinity);
min(), max()
min() returns the smallest of its comma-separated arguments and max() the largest. Together they set responsive limits without a media query.
| Syntax | min( <calc-sum># )max( <calc-sum># ) |
|---|---|
| Returns | The same type as its arguments |
| Support | Chrome 79 · Firefox 75 · Safari 11.1 |
| Spec | css-values-4 |
width: min(100%, 60rem); /* never wider than 60rem */
padding: max(1rem, 3vw); /* never less than 1rem */
clamp()
Keeps a preferred value between a minimum and a maximum: clamp(MIN, VAL, MAX) is the same as max(MIN, min(VAL, MAX)). Either bound can be none to leave that side open.
| Syntax | clamp( [ <calc-sum> | none ], <calc-sum>, [ <calc-sum> | none ] ) |
|---|---|
| Returns | The same type as its arguments |
| Support | Chrome 79 · Firefox 75 · Safari 13.1 |
| Spec | css-values-4 |
font-size: clamp(1rem, 0.5rem + 2vw, 2rem);
If the minimum is larger than the maximum, the minimum wins.
Stepped values
round()
Rounds a value to the nearest multiple of a step. The optional first argument is the rounding strategy: nearest (the default), up, down or to-zero.
| Syntax | round( <rounding-strategy>?, <calc-sum>, <calc-sum>? ) |
|---|---|
| Returns | The same type as its arguments |
| Support | Chrome 125 · Firefox 118 · Safari 15.4 |
| Spec | css-values-4 |
The step can be left out only when the value is a plain number, and then defaults to 1.
width: round(up, 37.5vw, 8px);
mod(), rem()
The modulus and the remainder of dividing the first argument by the second. They only differ when the signs differ: the result of mod() takes the sign of the divisor, the result of rem() the sign of the dividend, so mod(-5, 3) is 1 and rem(-5, 3) is -2.
| Syntax | mod( <calc-sum>, <calc-sum> )rem( <calc-sum>, <calc-sum> ) |
|---|---|
| Returns | The same type as its arguments |
| Support | Chrome 125 · Firefox 118 · Safari 15.4 |
| Spec | css-values-4 |
Trigonometric functions
sin(), cos(), tan()
The sine, cosine and tangent of an angle. The argument is an <angle>, or a plain number read as radians. Useful for placing items around a circle.
| Syntax | sin( <calc-sum> )cos( <calc-sum> )tan( <calc-sum> ) |
|---|---|
| Returns | <number> |
| Support | Chrome 111 · Firefox 108 · Safari 15.4 |
| Spec | css-values-4 |
translate: calc(cos(var(--angle)) * 8rem) calc(sin(var(--angle)) * 8rem);
asin(), acos(), atan()
The inverse trigonometric functions: each takes a number and returns the angle whose sine, cosine or tangent it is.
| Syntax | asin( <calc-sum> )acos( <calc-sum> )atan( <calc-sum> ) |
|---|---|
| Returns | <angle> |
| Support | Chrome 111 · Firefox 108 · Safari 15.4 |
| Spec | css-values-4 |
atan2()
The angle of the vector from the origin to the point (x, y), given as atan2(y, x). The two arguments can be any type as long as they match, so lengths work.
| Syntax | atan2( <calc-sum>, <calc-sum> ) |
|---|---|
| Returns | <angle> |
| Support | Chrome 111 · Firefox 108 · Safari 15.4 |
| Spec | css-values-4 |
rotate: atan2(var(--dy), var(--dx));
Exponential functions
pow()
Raises the first number to the power of the second. Handy for modular type scales.
| Syntax | pow( <calc-sum>, <calc-sum> ) |
|---|---|
| Returns | <number> |
| Support | Chrome 120 · Firefox 118 · Safari 15.4 |
| Spec | css-values-4 |
font-size: calc(1rem * pow(1.25, var(--step)));
sqrt()
The square root of a number.
| Syntax | sqrt( <calc-sum> ) |
|---|---|
| Returns | <number> |
| Support | Chrome 120 · Firefox 118 · Safari 15.4 |
| Spec | css-values-4 |
hypot()
The square root of the sum of the squares of its arguments: the length of a vector. Unlike sqrt(), the arguments can be lengths or any other matching type.
| Syntax | hypot( <calc-sum># ) |
|---|---|
| Returns | The same type as its arguments |
| Support | Chrome 120 · Firefox 118 · Safari 15.4 |
| Spec | css-values-4 |
log()
The logarithm of the first number. With one argument it is the natural logarithm; a second argument sets the base.
| Syntax | log( <calc-sum>, <calc-sum>? ) |
|---|---|
| Returns | <number> |
| Support | Chrome 120 · Firefox 118 · Safari 15.4 |
| Spec | css-values-4 |
exp()
The constant e raised to the power of the argument.
| Syntax | exp( <calc-sum> ) |
|---|---|
| Returns | <number> |
| Support | Chrome 120 · Firefox 118 · Safari 15.4 |
| Spec | css-values-4 |
Sign functions
abs()
The absolute value of its argument: the same value with any minus sign removed.
| Syntax | abs( <calc-sum> ) |
|---|---|
| Returns | The same type as its argument |
| Support | Chrome 138 · Firefox 118 · Safari 15.4 |
| Spec | css-values-4 |
sign()
The sign of its argument: -1 if negative, 1 if positive and 0 if zero. Useful for flipping a direction based on another value.
| Syntax | sign( <calc-sum> ) |
|---|---|
| Returns | <number> |
| Support | Chrome 138 · Firefox 118 · Safari 15.4 |
| Spec | css-values-4 |
Sizing, progress & tree counting
calc-size()
Does arithmetic on an intrinsic size such as auto, fit-content or max-content. The first argument is the basis; in the second, the keyword size stands for that basis. Because the result is a definite size, it can be transitioned, which is how you animate height to and from auto.
| Syntax | calc-size( <calc-size-basis>, <calc-sum> ) |
|---|---|
| Returns | A size, valid only in sizing properties such as width and height |
| Support | Chrome 129 · Firefox no · Safari no |
| Spec | css-values-5 |
height: calc-size(auto, size + 2rem);
progress()
How far a value lies between a start and an end, as a number: progress(value, start, end) is (value - start) / (end - start), clamped to the range 0 to 1. Pair it with other math to map one range onto another.
| Syntax | progress(no-clamp? <calc-sum>, <calc-sum>, <calc-sum>) |
|---|---|
| Returns | <number> |
| Support | Chrome 138 · Firefox 155 · Safari 26 |
| Spec | css-values-5 |
Firefox also accepts the leading no-clamp keyword, which lets the result go below 0 or above 1.
opacity: progress(100vw, 320px, 1200px);
random()
A random value between a minimum and a maximum, optionally rounded to a step given as the last argument. The optional first argument, a key, controls which uses share the same random value.
| Syntax | random( <random-key>? , <calc-sum>, <calc-sum>, <calc-sum>? ) |
|---|---|
| Returns | The same type as its arguments |
| Support | Chrome 156 · Firefox no · Safari 26.2 |
| Spec | css-values-5 |
rotate: random(-10deg, 10deg);
sibling-index(), sibling-count()
sibling-index() is the element's position among its parent's element children, starting at 1. sibling-count() is the number of those children, including the element itself. Both work inside other math, which makes staggered delays and evenly spread layouts easy.
| Syntax | sibling-index()sibling-count() |
|---|---|
| Returns | <integer> |
| Support | Chrome 138 · Firefox 154 · Safari 26.2 |
| Spec | css-values-5 |
li { transition-delay: calc((sibling-index() - 1) * 50ms); }