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.

Syntaxcalc( <calc-sum> )
ReturnsA number, dimension or percentage: whatever type the expression resolves to
SupportChrome 26 · Firefox 16 · Safari 7
Speccss-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:

ConstantValueSupport
eThe base of the natural logarithm, about 2.718Chrome 110 · Firefox 108 · Safari 15.4
piThe ratio of a circle's circumference to its diameter, about 3.142Chrome 109 · Firefox 108 · Safari 15.4
infinity, -infinityThe largest and smallest possible values; the result is clamped to the range the property allowsChrome 99 · Firefox 114 · Safari 16
NaNNot a number; a top-level NaN is treated as zeroChrome 99 · Firefox 114 · Safari 16
width: calc(100% - 2rem);
height: calc(100dvh - var(--header));
z-index: calc(infinity);
Full width minus a gutter, full viewport height minus a header, and the highest possible stacking level.

min(), max()

min() returns the smallest of its comma-separated arguments and max() the largest. Together they set responsive limits without a media query.

Syntaxmin( <calc-sum># )
max( <calc-sum># )
ReturnsThe same type as its arguments
SupportChrome 79 · Firefox 75 · Safari 11.1
Speccss-values-4
width: min(100%, 60rem);   /* never wider than 60rem */
padding: max(1rem, 3vw);    /* never less than 1rem */
A fluid value with a hard ceiling or floor.

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.

Syntaxclamp( [ <calc-sum> | none ], <calc-sum>, [ <calc-sum> | none ] )
ReturnsThe same type as its arguments
SupportChrome 79 · Firefox 75 · Safari 13.1
Speccss-values-4
font-size: clamp(1rem, 0.5rem + 2vw, 2rem);
Type that scales with the viewport but stays between 1rem and 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.

Syntaxround( <rounding-strategy>?, <calc-sum>, <calc-sum>? )
ReturnsThe same type as its arguments
SupportChrome 125 · Firefox 118 · Safari 15.4
Speccss-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);
Snap a fluid width up to the next multiple of 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.

Syntaxmod( <calc-sum>, <calc-sum> )
rem( <calc-sum>, <calc-sum> )
ReturnsThe same type as its arguments
SupportChrome 125 · Firefox 118 · Safari 15.4
Speccss-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.

Syntaxsin( <calc-sum> )
cos( <calc-sum> )
tan( <calc-sum> )
Returns<number>
SupportChrome 111 · Firefox 108 · Safari 15.4
Speccss-values-4
translate: calc(cos(var(--angle)) * 8rem) calc(sin(var(--angle)) * 8rem);
Place an item on a circle of radius 8rem.

asin(), acos(), atan()

The inverse trigonometric functions: each takes a number and returns the angle whose sine, cosine or tangent it is.

Syntaxasin( <calc-sum> )
acos( <calc-sum> )
atan( <calc-sum> )
Returns<angle>
SupportChrome 111 · Firefox 108 · Safari 15.4
Speccss-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.

Syntaxatan2( <calc-sum>, <calc-sum> )
Returns<angle>
SupportChrome 111 · Firefox 108 · Safari 15.4
Speccss-values-4
rotate: atan2(var(--dy), var(--dx));
Point an element along a vector.

Exponential functions

pow()

Raises the first number to the power of the second. Handy for modular type scales.

Syntaxpow( <calc-sum>, <calc-sum> )
Returns<number>
SupportChrome 120 · Firefox 118 · Safari 15.4
Speccss-values-4
font-size: calc(1rem * pow(1.25, var(--step)));
Each step up the scale is 1.25 times larger.

sqrt()

The square root of a number.

Syntaxsqrt( <calc-sum> )
Returns<number>
SupportChrome 120 · Firefox 118 · Safari 15.4
Speccss-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.

Syntaxhypot( <calc-sum># )
ReturnsThe same type as its arguments
SupportChrome 120 · Firefox 118 · Safari 15.4
Speccss-values-4

log()

The logarithm of the first number. With one argument it is the natural logarithm; a second argument sets the base.

Syntaxlog( <calc-sum>, <calc-sum>? )
Returns<number>
SupportChrome 120 · Firefox 118 · Safari 15.4
Speccss-values-4

exp()

The constant e raised to the power of the argument.

Syntaxexp( <calc-sum> )
Returns<number>
SupportChrome 120 · Firefox 118 · Safari 15.4
Speccss-values-4

Sign functions

abs()

The absolute value of its argument: the same value with any minus sign removed.

Syntaxabs( <calc-sum> )
ReturnsThe same type as its argument
SupportChrome 138 · Firefox 118 · Safari 15.4
Speccss-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.

Syntaxsign( <calc-sum> )
Returns<number>
SupportChrome 138 · Firefox 118 · Safari 15.4
Speccss-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.

Syntaxcalc-size( <calc-size-basis>, <calc-sum> )
ReturnsA size, valid only in sizing properties such as width and height
SupportChrome 129 · Firefox no · Safari no
Speccss-values-5
height: calc-size(auto, size + 2rem);
The content height plus 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.

Syntaxprogress(no-clamp? <calc-sum>, <calc-sum>, <calc-sum>)
Returns<number>
SupportChrome 138 · Firefox 155 · Safari 26
Speccss-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);
Fade in as the viewport widens from 320px to 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.

Syntaxrandom( <random-key>? , <calc-sum>, <calc-sum>, <calc-sum>? )
ReturnsThe same type as its arguments
SupportChrome 156 · Firefox no · Safari 26.2
Speccss-values-5
rotate: random(-10deg, 10deg);
Tilt each card by a random angle.

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.

Syntaxsibling-index()
sibling-count()
Returns<integer>
SupportChrome 138 · Firefox 154 · Safari 26.2
Speccss-values-5
li { transition-delay: calc((sibling-index() - 1) * 50ms); }
Each list item starts 50ms after the one before.