Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 139 ยท Values and Units

Advanced Math Functions: round(), mod() and Trigonometry

Go beyond calc() with CSS round(), mod(), rem(), abs(), sign(), pow(), sqrt(), hypot() and trigonometry. Snap values to steps and place items on a circle.

In calc(), min(), max() and clamp() you learned the four math functions you'll use every day. CSS has quite a few more. Some round a value to a step, some return a remainder, some work with signs and powers, and a whole family does trigonometry, so you can place things on a circle or point an arrow at a target without a single line of JavaScript math.

You won't need these on every project. But when you do, they replace awkward hacks: hard-coded positions for each item of a circular menu, JavaScript that rounds a size to a grid, or a preprocessor that pre-computes sine tables. In this lesson you'll learn what each function does, the rules they share, and a handful of patterns where they shine. Several examples store numbers in custom properties, so make sure you're comfortable with var() first.

The rules they all share

Every function in this lesson is a math function, just like calc(). That gives them the same rules:

  • You can use them anywhere a number, length, angle, percentage or time is allowed, and nest them inside each other and inside calc().
  • Each argument can be a full expression: round(100% / 3, 1px) needs no inner calc().
  • Arguments that are combined must have compatible types. mod(10px, 3) is invalid, because it mixes a length with a plain number.
  • The result has a type too. sin() returns a plain number, so you multiply it by a length to get a distance: calc(sin(30deg) * 100px) is 50px.

CSS also has a few named constants that work inside any math function: pi, e, infinity and -infinity (plus NaN, mainly for completeness). For example, calc(2 * pi * 40px) is the circumference of a circle with a 40px radius.

Here is the whole family at a glance:

Function Returns Example Result
round(strategy?, A, B?) A rounded to a multiple of B round(37px, 10px) 40px
mod(A, B) Remainder, with the sign of B mod(-7px, 5px) 3px
rem(A, B) Remainder, with the sign of A rem(-7px, 5px) -2px
abs(A) A without its sign abs(-12px) 12px
sign(A) -1, 0 or 1 sign(-12px) -1
pow(A, B) A to the power B (numbers only) pow(2, 5) 32
sqrt(A) Square root (numbers only) sqrt(2) 1.414โ€ฆ
hypot(A, B, โ€ฆ) Length of a vector hypot(30px, 40px) 50px
log(A, B?) Logarithm, natural by default log(100, 10) 2
exp(A) e to the power A exp(1) 2.718โ€ฆ
sin(), cos(), tan() A plain number cos(60deg) 0.5
asin(), acos(), atan(), atan2(A, B) An angle atan2(1px, 1px) 45deg

round(): snapping to a step

round() takes a value A and a rounding interval B, and returns the multiple of B that's closest to A. With an optional first keyword, you choose the direction:

Strategy Picks round(โ€ฆ, 37px, 10px)
nearest (default) the closest multiple; exact halves go up 40px
up the next multiple toward positive infinity 40px
down the next multiple toward negative infinity 30px
to-zero the next multiple toward zero 30px

up and down describe the number line, not "away from zero", so round(down, -37px, 10px) is -40px while round(to-zero, -37px, 10px) is -30px.

The interval can only be left out when A is a plain number, in which case it defaults to 1: round(2.6) is 3. For lengths you must always give a step: round(37px) is invalid.

Drag the slider to change the raw width and watch each strategy snap to the 20px grid:

round() strategies on a 20px grid

At 147px, nearest gives 140px (147 is closer to 140 than to 160), up gives 160px and down gives 140px. Move the slider to 150 and nearest jumps to 160px, because exact halves round up.

A practical use is keeping sizes on a spacing grid. A fluid value like clamp(1rem, 3vw, 2rem) can land on any fraction of a pixel; wrapping it in round(โ€ฆ, 4px) keeps every gap a clean multiple of 4px.

mod() and rem(): remainders

Both functions divide A by B and return what's left over. They differ only for negative values:

  • mod(A, B) takes the sign of B. mod(-7, 5) is 3, which is what you want for wrapping around: one step back from 0 on a five-step cycle lands on step 4, and so on.
  • rem(A, B) takes the sign of A. rem(-7, 5) is -2, which matches the % operator in JavaScript, C and Java.

For positive values they agree: mod(17deg, 5deg) and rem(17deg, 5deg) are both 2deg. Don't confuse the rem() function with the rem unit; they share a name and nothing else.

mod() is handy for cycles. If each item has an index in --i, then mod(var(--i), 3) counts 0, 1, 2, 0, 1, 2โ€ฆ, which you can turn into a repeating color, offset or delay.

abs(), sign() and the exponential functions

abs() removes the sign, and sign() reports it as -1, 0 or 1. Because sign() returns a plain number, it's useful for flipping a direction: calc(sign(var(--offset)) * 20px) moves 20px in whichever direction --offset points, however big it is.

pow(), sqrt(), log() and exp() work on plain numbers only. That's fine, because you can multiply the result by a unit. A modular type scale is the classic example: each heading level is the base size times a ratio raised to a power.

CSS
:root { --ratio: 1.25; }
h3 { font-size: calc(1rem * pow(var(--ratio), 1)); }  /* 1.25rem */
h2 { font-size: calc(1rem * pow(var(--ratio), 2)); }  /* 1.5625rem */
h1 { font-size: calc(1rem * pow(var(--ratio), 3)); }  /* 1.953125rem */

hypot() is the exception: it accepts lengths, as long as they all have the same type. hypot(30px, 40px) is the length of the diagonal of a 30 ร— 40 rectangle, 50px.

The playground below evaluates any expression with real CSS. It writes your expression into a property and reads back what the browser computed, so you see exactly what your browser does. Click the examples or type your own:

Math function playground

Try mod(10px, 3) to see a type error, or round(37px) to see that a length needs an interval. Note how log() and pow() are multiplied by 1px to turn their plain-number results into lengths.

Trigonometry: sin(), cos() and friends

sin(), cos() and tan() take an angle such as 30deg, 0.5turn or 1rad (a plain number is treated as radians) and return a plain number. The inverse functions go the other way: asin(), acos() and atan() take a number and return an angle, and atan2(A, B) takes two values of the same type and returns the angle of the point (B, A).

The most common reason to use them is placing items on a circle. A point at angle ฮธ on a circle of radius r sits at x = cos(ฮธ) ร— r and y = sin(ฮธ) ร— r, measured from the center. In CSS, y grows downward, so increasing angles go clockwise.

A clock face with cos() and sin()

Every label shares one rule. Only --i differs, and the CSS turns it into an angle and then into a position. Change --radius to 90px and the whole face shrinks with no other edits. Spreading items evenly works the same way with any count: use 360deg / var(--count) * var(--i) as the angle.

Setting --i on every item by hand is the one tedious part. A newer function, sibling-index(), returns an element's position among its siblings and would remove that step, but it's only in Chromium browsers at the time of writing. Server templates or a small loop in your HTML generator are the portable way.

Pointing at something with atan2()

atan2(A, B) answers the question "which direction is this point from here?" Give it the vertical and horizontal distance, and it returns the angle. Combine it with hypot() for the distance, and CSS does all the geometry. In this demo, JavaScript only reports where the pointer is; the angle and the length of the line are worked out in CSS:

An arrow that follows the pointer

Before you move the pointer, the beam starts at the preset --dx: 80px and --dy: -40px: about 89.4px long, rotated about โˆ’26.6 degrees (up and to the right). Note the argument order: atan2() takes the vertical distance first, like its counterpart in nearly every programming language.

Stepped layouts with round()

round() also works with percentages, so a box can grow in whole steps instead of smoothly. Below, a shelf only gets wider in units of one 60px "slot", so items never end up half visible. Drag the slider to change the available space:

A shelf that grows in whole slots

At 330px the shelf is 300px wide: five full slots, with the leftover 30px unused. The same idea snaps a hero image's height to a baseline grid or keeps a sidebar width on a column grid.

You might want round(down, 100% / 60px) to get a slot count instead of a width. Dividing a length by a length to get a plain number is part of the specification, and recent Chromium versions allow it, but not every browser does yet, so use the width-based version above for now.

Browser support and fallbacks

  • Trigonometry (sin() through atan2()) and pow(), sqrt(), hypot(), log(), exp() have worked in all major browsers since 2023.
  • round(), mod() and rem() have worked in all major browsers since 2024.
  • abs() and sign() arrived last: Firefox and Safari had them for years, but Chrome and Edge only added them in mid-2025.

Older browsers drop a declaration they don't understand, so the usual fallback pattern works: write a simpler value first and the math version second.

CSS
.label {
  translate: 0 -120px;                                  /* older browsers */
  translate: calc(cos(var(--a)) * 120px) calc(sin(var(--a)) * 120px);
}

For whole blocks of rules, test with a feature query such as @supports (width: round(1px, 1px)).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is round(down, -37px, 10px)?

    down rounds toward negative infinity, so -37px goes to -40px. Use to-zero to get -30px.
  2. 2. Which pair gives different results?

    For a negative dividend, mod() takes the sign of the divisor (3) and rem() takes the sign of the dividend (-2). The other pairs are equal.
  3. 3. Why is width: calc(sin(30deg) * 200px) valid, while width: sin(30deg) is not?

    Trigonometric functions return plain numbers. Multiplying by 200px turns 0.5 into the length 100px.
  4. 4. An item should sit on a 100px circle at the angle stored in --a. Which translate value places it?

    x is cos(angle) ร— radius and y is sin(angle) ร— radius. The functions take an angle, not a length, so the radius multiplies the result.
  5. 5. Which declaration is invalid?

    pow() only accepts plain numbers. Write calc(pow(2, 3) * 1px) instead.

Summary

  • The advanced math functions follow the same rules as calc(): use them anywhere a numeric value fits, nest them freely, and keep argument types compatible.
  • round(strategy, A, B) snaps A to a multiple of B with nearest, up, down or to-zero; lengths always need the interval B.
  • mod() returns a remainder with the sign of the divisor (good for cycles); rem() uses the sign of the dividend, like % in most languages.
  • abs() and sign() handle signs; pow(), sqrt(), log() and exp() work on plain numbers, while hypot() accepts lengths.
  • sin(), cos() and tan() turn angles into numbers, perfect for placing items on a circle; atan2(y, x) turns distances into a direction.
  • The constants pi and e work inside any math function.

Next: The Box Model โ€” how content, padding, border and margin add up to an element's size.