mod() CSS Function
The mod() function returns the modulus of dividing one value by another, taking the sign of the second value, such as mod(-7, 3), which is 2.
Description
The mod() CSS function returns the modulus left over when the first argument (the dividend) is divided by the second argument (the divisor). It is similar to rem(), but the result always takes the sign of the divisor. That makes mod() the right choice for "wrapping" values into a range: with a positive divisor the answer is always positive, even when the dividend is negative. For example, mod(-140deg, 360deg) is 220deg, while rem(-140deg, 360deg) is -140deg.
Both arguments must be of the same type, and the result has that type. If the divisor is 0, the result is NaN. A common use is keeping hue angles or positions within a fixed cycle, such as wrapping a color hue into the range 0 to 360 degrees or making a repeating pattern from an index value.
/* Wrap any hue into the 0deg to 360deg range */
.swatch {
background: hsl(mod(var(--hue), 360deg) 80% 50%);
}
/* Alternate a value every 3 items using an index */
.item {
translate: 0 calc(mod(var(--i), 3) * 10px);
}
mod() is one of the stepped value functions, alongside round() and rem().
Interactive Demo
Syntax
mod() = mod( <calc-sum>, <calc-sum> )
Values
- dividendA calculation resolving to a <number>, <dimension> or <percentage>. The value to be divided.
- divisorA calculation of the same type as the dividend. The value to divide by. The result has the same sign as the divisor.
Example
Browser Support
The following information will show you the current browser support for the CSS mod() function. Hover over a browser icon to see the version that first introduced support for this CSS function.
Desktop





Tablets & Mobile





