Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

mod() — modulo with the divisor’s sign

Wrap a CSS value into a repeating interval and understand why negative values differ from remainder arithmetic.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

mod(A, B)
transform: translateX(mod(-18px, 5px));

mod(A, B) moves A by a whole-number multiple of B into the interval between zero and B, including zero but excluding B. For finite arguments with a nonzero divisor, a nonzero result has the sign of B. mod() accepts compatible numeric types, including lengths; mixing a length with a time is invalid.

mod(-18px, 5px) yields 2 pixels, whereas mod(18px, -5px) yields −2 pixels. The related rem() instead keeps the dividend’s sign for a nonzero result. JavaScript’s % follows remainder behavior, so copying its negative-value result into CSS modulo logic can give the wrong position or phase.

A zero divisor or an infinite dividend produces NaN; infinity has additional rules in the specification. Keep practical inputs finite and the divisor nonzero. CSS.supports() returning true for a zero-divisor declaration confirms parsing, not a useful result. Negative outputs are valid for translations, while properties that prohibit negative values can conceal them through range restrictions.

Examples

const box = document.createElement('div');
document.body.append(box);
function offset(expression) {
  box.style.transform = 'translateX(' + expression + ')';
  return new DOMMatrix(getComputedStyle(box).transform).m41;
}
for (const [a, b] of [[18,5],[-18,5],[18,-5],[-18,-5]]) {
  console.log(a + ',' + b + ':',
    offset('mod(' + a + 'px,' + b + 'px)'),
    offset('rem(' + a + 'px,' + b + 'px)'));
}
console.log('mixed types:', CSS.supports('width', 'mod(18px,5s)'));
console.log('zero divisor parses:', CSS.supports('width', 'mod(18px,0px)'));
box.remove();

Result

18,5: 3 3
-18,5: 2 -3
18,-5: -2 3
-18,-5: -3 -3
mixed types: false
zero divisor parses: true

The displayed code reads real translation matrices and prints modulo then remainder for all four sign combinations. It separately tests incompatible dimensions and a zero divisor; the zero-divisor declaration is not applied to the element.

Sources