Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

round(): snap a value to a chosen interval

Choose a rounding strategy explicitly, especially for negative values and halfway cases.

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

Syntax

margin-left: round(nearest, -15px, 10px);

The function round() rounds a value to an integer multiple of an interval. Arguments must have compatible types: lengths can be rounded to lengths, but not to seconds. The default strategy nearest chooses the closer multiple; an exact tie goes towards positive infinity. The other strategies are up towards positive infinity, down towards negative infinity, and to-zero towards zero. For negative inputs, up and down are not interchangeable with increasing or reducing the magnitude.

With an interval of 10px, -15px becomes -10px for nearest, up and to-zero, but -20px for down. A value already on a multiple stays unchanged. A zero interval produces NaN, so use a meaningful nonzero interval. A unitless number may omit the interval and then uses 1; dimensions normally need an explicit interval. The current draft also defines line-width for lengths with special pixel snapping, but this newer strategy needs its own compatibility check.

The example measures computed margins for all four established strategies and both signs of the halfway case: positive 15px becomes 20px. It also preserves exact -20px and rejects a mixture of px and s. Chromium 157 returns false for the tested round(line-width, 1.3px) syntax. That implementation limit does not make the draft feature invalid or prevent the established strategies from working.

Examples

const box=document.createElement('div');document.body.append(box);
const read=value=>{box.style.marginLeft='0px';box.style.marginLeft=value;return getComputedStyle(box).marginLeft};
for(const mode of ['nearest','up','down','to-zero'])console.log(mode+'='+read('round('+mode+', -15px, 10px)'));
console.log('positive-tie='+read('round(15px, 10px)')+'; exact='+read('round(-20px, 10px)'));
console.log('mixed-types='+CSS.supports('margin-left','round(15px, 2s)')+'; line-width='+CSS.supports('border-width','round(line-width, 1.3px)'));

Result

nearest=-10px
up=-10px
down=-20px
to-zero=-10px
positive-tie=20px; exact=-20px
mixed-types=false; line-width=false

The results show that up means towards positive infinity, not away from zero. The separate line-width probe is a compatibility result, not an executed pixel-snapping demonstration.

Sources