CSS Reference ยท Functions
The abs() and sign() functions
Reference for the CSS abs() and sign() functions: absolute values of lengths and numbers, getting the sign of a value, syntax and live examples.
abs() returns the absolute value of its argument: the same magnitude with the sign removed. sign() returns the sign: -1, 0 or 1 (as a plain number), whatever the unit of the argument. They are small building blocks for formulas where a value might be negative, such as offsets that are set from a custom property.
.marker { width: calc(abs(var(--delta)) + 10px); translate: calc(sign(var(--delta)) * 20px) 0; }
Try it
All three bars have a width that grows with the distance from zero, not with the signed value.
Syntax
abs( <calc-sum> ) /* same type as the argument, never negative */ sign( <calc-sum> ) /* a number: -1, 0 or 1 (also -0 and +0 are kept) */
- Result type:
abs()keeps the type of its argument (length, percentage, angle, number...).sign()always returns a unitless number. - Inherited / animatable: not applicable.
Syntax and arguments
| Function | Description |
|---|---|
abs(A) |
Returns A if it is zero or positive, otherwise -A. abs(-5px) is 5px; abs(3) is 3. |
sign(A) |
Returns -1 for negative values, 1 for positive values and 0 for zero. sign(-3em) is -1. |
The argument can be a full expression, including other math functions and custom properties.
Usage notes
- Directions from one number. Multiply a fixed amount by
sign()to move left or right, up or down, or to switch rotation direction depending on a custom property. - Distance from a center.
abs(var(--x) - 50)is the distance from a midpoint, useful for effects that get stronger away from the middle. - Combine them. For any value,
abs(x) * sign(x)isxagain, so you can modify the magnitude separately from the direction. - Resolved at computed-value time. If the argument contains percentages that depend on layout, the result adjusts when layout changes.
- Not a clamp. To limit a value to a range, use
clamp(),min()ormax(). - Fallbacks. Where unsupported, the whole declaration is dropped. Put a safe static value before it.
Browser support
abs() and sign() are among the newer CSS math functions; the current versions of the major browsers support them, but check a compatibility table before relying on them.
Related
calc()round(),mod(),rem()min(),max(),clamp()- Lesson: Math functions