Favorites

mod() CSS Function

mod()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

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.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

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

050px100px150px200px250px
Demo Code
HTML Code
<div class="demo-track">
	<div class="demo-element"></div>
</div>
<div class="demo-scale"><span>0</span><span>50px</span><span>100px</span><span>150px</span><span>200px</span><span>250px</span></div>
CSS Code
.demo-track {
	width: 300px;
	height: 36px;
	background: #f4f4f4 repeating-linear-gradient(90deg, #b0b0b0 0 1px, transparent 1px 50px);
	outline: 1px solid #c5c5c5;
}
.demo-scale {
	display: grid;
	grid-template-columns: repeat(6, 50px);
	width: 300px;
	font-size: 11px;
	color: #666;
}
.demo-element {
	height: 100%;
	background: linear-gradient(90deg, #f72585, #b5179e);
	width: mod(270px, 120px);
}

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

<div class="wrap">
<h1>CSS mod() examples</h1>
<div class="grid">
<div class="card">
<div class="sw a"></div>
<div class="label">mod(480deg, 360deg) = 120deg</div>
</div>
<div class="card">
<div class="sw b"></div>
<div class="label">mod(-140deg, 360deg) = 220deg</div>
</div>
<div class="card">
<div class="sw c"></div>
<div class="label">mod(750deg, 360deg) = 30deg</div>
</div>
</div>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* mod() examples */
.sw {
height: 80px;
border-radius: 8px;
background: hsl(mod(var(--hue), 360deg) 75% 50%);
}

.a { --hue: 480deg; } /* wraps to 120deg (green) */
.b { --hue: -140deg; } /* wraps to 220deg (blue) */
.c { --hue: 750deg; } /* wraps to 30deg (orange) */
Open in editor

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.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee