CSS can round now, and on 17 November it became safe to use everywhere: round(), mod() and rem() reached Baseline Widely available, two and a half years after the last major engine shipped them. They're small functions, but they move a whole category of "snap this to a step" logic out of JavaScript.
The demo above is a snapping lab. The teal point moves freely, and JavaScript only writes its position into two custom properties. The orange node is placed by CSS alone, with round(). Change the rounding strategy or the step and watch where the node lands. Move the point to the left of the centre and the mod() and rem() bars stop agreeing.
What they do
The specification calls them stepped-value functions: each one transforms a value according to a step.
round(<strategy>?, A, B)returns A rounded to a multiple of B.mod(A, B)andrem(A, B)return what's left of A after you remove whole multiples of B.
They work with numbers, lengths, angles, times and percentages. The one rule is that both arguments must have a consistent type: round(10px, 2) is invalid, and so is round(10px). For a plain number you can leave out B and it defaults to 1. For anything with a unit, B is required. I checked both invalid cases: all three engines drop the declaration.
round() and its four strategies
This is the CSS the demo uses for the node:
.stage { --x: 0px; --y: 0px; --step: 40px; --mode: nearest; }
.node {
translate:
round(var(--mode), var(--x), var(--step))
round(var(--mode), var(--y), var(--step));
}
The strategy is the optional first argument, and as the demo shows, it can come from a custom property too. The spec defines four of them:
nearest(the default): the closest multiple. If A is exactly halfway, it picks the upper one.up: always the multiple towards +∞, likeMath.ceil().down: always towards −∞, likeMath.floor().to-zero: the multiple closer to zero, likeMath.trunc().
On screen, "up" means right and down, because the y axis grows downwards. That's why up and down look odd above the centre line in the demo. And because nearest breaks ties towards +∞, round(-105px, 10px) gives -100px, not -110px.
The demo is a toy, but the same idea works in layouts. Rounding against a percentage resolves it first, so this keeps a container at an exact multiple of a column width:
.gallery {
width: round(down, 100%, 180px); /* 1000px of space → 900px */
margin-inline: auto;
}
MDN makes a useful point: rounding only makes sense when at least one of the values isn't known in advance. That means a custom property, a percentage or a viewport unit. round(117px, 25px) is valid, but you could just write 125px.
mod() vs rem(): it's all about the sign
When A and B have the same sign, both functions give the same result. The spec's examples show where they differ:
translate: mod(-18px, 5px); /* 2px: takes the sign of the step */
translate: rem(-18px, 5px); /* -3px: takes the sign of A, like JS's % */
rotate: mod(140deg, -90deg); /* -40deg */
rotate: rem(140deg, -90deg); /* 50deg */
I ran all four in Chromium, Firefox and WebKit, and the computed values match the spec. In JavaScript, -18 % 5 is -3 too, and the spec gives exactly that as a reason to use rem(): when CSS has to match JS code.
Most of the time you want mod(). You control the step and the input is unknown, so you want a result that always stays between 0 and the step. mod(-450deg, 360deg) gives 270deg, a clean normalised angle. The demo makes this easy to see: drag the point left of the vertical axis and the rem() dot jumps to the negative side, while the mod() dot stays on the positive side.
Browser support and gotchas
According to webstatus.dev, Firefox 118 shipped all three functions first (September 2023), then Safari 17.2 (December 2023), and Chrome and Edge 125 (May 2024). That made them Baseline Newly available on 17 May 2024 and Widely available on 17 November 2026. The demo still checks CSS.supports('translate', 'round(down, 7px, 3px)') and computes the values in JavaScript if that fails, but under Baseline Widely available that path should almost never run.
- You can't divide units away yet.
round()gives you120px, not "column 3". Turning a length into a plain number needs typed arithmetic, which I covered in another note. It's much newer and still needs a fallback. The demo calculates the column in JavaScript for its screen reader message. - Watch the types. Mixing a length with a bare number makes the whole declaration invalid, and the browser ignores it without any warning. If something doesn't snap, check this first.
- The computed value is already rounded.
getComputedStyle()returns the final value (120px), which is how the demo fills its readouts. If the property has a transition, as the node does, read from an element without one, or you'll get the in-between value. - Floating point still applies.
opacity: round(0.37, 0.1)gives0.4in all three engines, but don't build equality checks on top of non-integer steps.
What to keep an eye on
- Typed arithmetic reaching Widely available. Combined with
round()andmod(), it would let CSS calculate "which cell" and "odd or even" on its own. Patterns like striping by position wouldn't need JavaScript any more. - Design tokens on a step. A spacing or type scale snapped with
round()to a 4px or 8px grid, even when the base value is fluid, is now a one-liner instead of a build step.