Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

rem(): a remainder that follows the input sign

Calculate a typed remainder and distinguish it from mod() and the rem font-relative unit.

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

Syntax

transform: translateX(rem(-17.5px, 4px));

The function rem(A, B) subtracts a whole-number multiple of the second argument from the first. For finite arguments and a nonzero divisor, a nonzero result has the sign of A and a magnitude smaller than the absolute value of B. The two calculations must have a compatible CSS numeric type; the result keeps that type. This function is unrelated to the root-font unit in 2rem.

Unlike rem(), mod() follows the divisor’s sign when the result is nonzero. A positive cycle often needs the latter, while a signed displacement may need the former. A zero divisor or an infinite first argument produces a calculation result of NaN, not an ordinary remainder. A successful CSS.supports() check only establishes that the browser accepts the declaration’s syntax: it does not make those exceptional inputs meaningful.

The example measures actual horizontal translations. The negative input produces -1.5px with the remainder function and 2.5px with the modulus function; changing only the divisor’s sign gives 1.5px for the positive input. An exact multiple gives zero. Mixing lengths with seconds is rejected, but the zero-divisor declaration parses. These observations do not measure the final property-specific treatment of NaN.

Examples

const box = document.createElement('div');
document.body.append(box);
function distance(expression) {
  box.style.transform = 'translateX(' + expression + ')';
  return new DOMMatrix(getComputedStyle(box).transform).m41;
}
console.log('negative input:', distance('rem(-17.5px,4px)'));
console.log('same input with mod:', distance('mod(-17.5px,4px)'));
console.log('negative divisor:', distance('rem(17.5px,-4px)'));
console.log('exact multiple:', distance('rem(12px,4px)'));
console.log('incompatible units:', CSS.supports('width', 'rem(12px,4s)'));
console.log('zero divisor parses:', CSS.supports('width', 'rem(12px,0px)'));
box.remove();

Result

negative input: -1.5
same input with mod: 2.5
negative divisor: 1.5
exact multiple: 0
incompatible units: false
zero divisor parses: true

Values come from computed transform matrices in Chromium 157. The zero-divisor probe checks parsing only; it deliberately does not present a NaN calculation as a valid displacement.

Sources