Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Functions

The round(), mod() and rem() functions

Reference for the CSS round(), mod() and rem() functions: rounding values to a step, remainders with different signs, syntax, examples and support notes.

These three math functions work with steps. round() snaps a value to a multiple of an interval, mod() and rem() return the remainder of a division. They accept lengths, percentages, angles, times and plain numbers, and can be used anywhere a value of that type is allowed, including inside calc().

CSS
.card {
  width: round(down, 100%, 80px);   /* snap to a multiple of 80px */
}

Try it

Snapping a width to a step

If your browser shows the bars all at full width or without color, it does not support these functions yet.

Syntax

CSS
round( <strategy>? , <value> , <interval>? )
mod( <dividend> , <divisor> )
rem( <dividend> , <divisor> )

/* <strategy>: nearest | up | down | to-zero   (default: nearest) */
  • Result type: the same type as the arguments. Arguments must have matching types (all lengths, all numbers, and so on).
  • Interval: if omitted in round(), it defaults to 1 only for plain numbers; for other types you must provide it.
  • Inherited / animatable: not applicable; these are value functions.

Values

Function Description
round(nearest, A, B) The multiple of B nearest to A. Ties round up toward positive infinity.
round(up, A, B) The nearest multiple of B greater than or equal to A.
round(down, A, B) The nearest multiple of B less than or equal to A.
round(to-zero, A, B) The nearest multiple of B toward zero (truncation).
mod(A, B) Remainder of A / B with the sign of the divisor B. Always between 0 and B.
rem(A, B) Remainder with the sign of the dividend A, like the % operator in many languages.

Examples: round(17px, 5px) is 15px, round(up, 17px, 5px) is 20px, mod(18px, 5px) is 3px. They differ for negative dividends: mod(-18px, 5px) is 2px, while rem(-18px, 5px) is -3px.

Usage notes

  • Pixel-snapped layout. Snap a fluid width to a grid of columns or sprite cells so tiles line up without a fractional edge.
  • Responsive text sizes: round(1.1vw, 1px) avoids a size that changes by tiny fractions on every resize.
  • Cycles and wrapping. mod() is natural for wrapping an index or an angle: mod(var(--a), 360deg).
  • Dividing by zero (a 0 interval or divisor) produces NaN, which makes the declaration invalid at computed-value time.
  • Percentages can be used with lengths; they are resolved as in calc(), so the property must allow percentages.
  • No unit conversion trick: mod(1px, 1) mixes a length and a number and is invalid.

Browser support

Stepped value functions are newer than calc(); they are supported in the current versions of the major browsers, but check a compatibility table before depending on them. Provide a plain fallback declaration before the one using round().