Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

sign(): direction without magnitude

Reduce a numeric calculation to its sign and use the unitless result as a multiplier.

Version: CSS Modules

Syntax

transform: translateX(calc(sign(-8px) * 12px));

sign(A) evaluates one calculation and returns -1 for a negative value, +1 for a positive value, and zero for zero. Its result is a number without a length or angle unit, even when the argument is a length or angle. It therefore describes direction rather than size: sign(-8px) is -1, not -1px or 8px. abs() has a different purpose and preserves the argument’s magnitude.

The sign is taken after the argument has been resolved. A positive percentage is not necessarily a positive resolved length: for background-position, the reference difference becomes negative when the image is larger than the positioning area. The specification also preserves the distinction between positive and negative zero, while NaN remains NaN. A displayed decimal zero in this example does not test signed-zero behavior.

The example multiplies each sign by 12px for a horizontal translation. Arguments -8px, 0px and 8px produce offsets -12, 0 and 12 pixels; -3deg also gives -12 pixels because its sign is unitless. The browser rejects width: sign(8px), since a nonzero unitless number is not a width. The measured transform matrices confirm actual evaluation.

Examples

const box=document.createElement('div');document.body.append(box);
const move=value=>{box.style.transform='translateX(calc(sign('+value+') * 12px))';return new DOMMatrixReadOnly(getComputedStyle(box).transform).m41.toFixed(0)};
const negative=move('-8px'),zero=move('0px'),positive=move('8px'),angle=move('-3deg');
console.log('negative='+negative+'; zero='+zero+'; positive='+positive+'; angle='+angle+'; bare-width='+CSS.supports('width','sign(8px)')+'; supported='+CSS.supports('transform','translateX(calc(sign(-8px) * 12px))'));

Result

negative=-12; zero=0; positive=12; angle=-12; bare-width=false; supported=true

Verified in Chromium 157.0.8103.51. A fallback or @supports is needed when targeting browsers without this math function. Percent-resolution and signed-zero cases are explained from the specification, not claimed as executed by this example.

Sources