sign() CSS Function
The sign() function returns -1 if a value is negative, 1 if it is positive, and 0 if it is zero.
Description
The sign() CSS function returns the sign of its argument: -1 if the value is negative, 1 if it is positive, and 0 if it is zero (strictly, 0 or -0). The argument can be any numeric type, such as a length, angle or number, but the result is always a plain <number>.
Because the result is just -1, 0 or 1, sign() is useful as a switch inside calculations. You can multiply another value by it to flip a direction, turn something on or off, or pick between two states based on whether a custom property is positive or negative, all without media queries or JavaScript. For instance, an arrow can point up for a positive change and down for a negative change from a single custom property.
/* Arrow points up for gains and down for losses */
.trend {
rotate: calc(90deg - sign(var(--change)) * 90deg);
}
/* Move in the direction of an offset, by a fixed amount */
.nudge {
translate: calc(sign(var(--offset)) * 8px) 0;
}
sign() pairs naturally with abs(), since calc(sign(x) * abs(x)) always equals x.
Interactive Demo
Syntax
sign() = sign( <calc-sum> )
Values
- <calc-sum>A calculation resolving to a <number>, <dimension> or <percentage>. The function returns -1 if it is negative, 1 if positive and 0 if zero.
Example
Browser Support
The following information will show you the current browser support for the CSS sign() function. Hover over a browser icon to see the version that first introduced support for this CSS function.
Desktop





Tablets & Mobile





