The mod()
CSS function returns a modulus left over when the first parameter is divided by the second parameter, similar to the JavaScript remainder operator (%
). The modulus is the value left over when one operand, the dividend, is divided by a second operand, the divisor. It always takes the sign of the divisor.
For example, the CSS mod(21, -4)
function returns the remainder of -1
. When dividing 21 by -4, the result is 5 with a remainder of -1. The full calculation is 21 / -4 = -4 * 5 - 1
.
Syntax
line-height: mod(7, 2);
line-height: mod(14, 5);
line-height: mod(3.5, 2);
margin: mod(15%, 2%);
margin: mod(18px, 4px);
margin: mod(19rem, 5rem);
margin: mod(29vmin, 6vmin);
margin: mod(1000px, 29rem);
rotate: mod(100deg, 30deg);
rotate: mod(135deg, -90deg);
rotate: mod(-70deg, 20deg);
rotate: mod(-70deg, -15deg);
scale: mod(10 * 2, 1.7);
rotate: mod(10turn, 18turn / 3);
transition-duration: mod(20s / 2, 3000ms * 2);
Parameter
The mod(dividend, divisor)
function accepts two comma-separated values as its parameters. Both parameters must have the same type, number, dimension, or percentage, for the function to be valid. While the units in the two parameters don't need to be the same, they do need of the same dimension type, such as <length>
, <angle>
, <time>
, or <frequency>
to be valid.
-
dividend
-
A calculation that resolves to a <number>
, <dimension>
, or <percentage>
representing the dividend.
-
divisor
-
A calculation that resolves to a <number>
, <dimension>
, or <percentage>
representing the divisor.
Return value
Returns a <number>
, <dimension>
, or <percentage>
(corresponds to the parameters' type) representing the modulus, that is, the operation left over.
- If
divisor
is 0
, the result is NaN
.
- If
dividend
is infinite
, the result is NaN
.
- If
divisor
is positive the result is positive (0⁺
), and if divisor
is negative the result is negative (0⁻
).
<mod()> =
mod( <calc-sum> , <calc-sum> )
<calc-sum> =
<calc-product> [ [ '+' | '-' ] <calc-product> ]*
<calc-product> =
<calc-value> [ [ '*' | '/' ] <calc-value> ]*
<calc-value> =
<number> |
<dimension> |
<percentage> |
<calc-constant> |
( <calc-sum> )
<calc-constant> =
e |
pi |
infinity |
-infinity |
NaN
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
mod |
No |
No |
118 |
No |
No |
15.4 |
No |
No |
118 |
No |
15.4 |
No |