# abs()

> The `abs()` CSS function returns the absolute value of the argument.

## What does abs() do in CSS?

The function that returns the absolute value of a number, length or other numeric value, removing its sign. Useful inside `calc()` expressions where the result may become negative.

- Topic: Mathematics
- Type: function
- Browser support: Chrome 138+, Chrome Android 138+, Edge 138+, Firefox 118+, Firefox Android 118+, Safari 15.4+, Safari iOS 15.4+

## Example

Wrong:

```css
.item {
  --dist: max(var(--offset), -1 * var(--offset));
}
```

Right:

```css
.item {
  --dist: abs(var(--offset));
}
```

`abs()` returns the positive value directly, without the `max()` trick.

## Common mistakes

- The whole declaration is dropped in browsers without `abs()`, because there was no fallback.

## Key points

- `abs()` is used instead of hacks like `max(x, -1 * x)` to get a positive value.
- The argument can hold math directly, like `abs(50% - 100px)`, without an extra `calc()`.
- A fallback declaration comes before the `abs()` one where support is missing.

## Interview questions

### Does abs() keep the unit of the argument?

Yes. `abs(-20px)` returns '20px', with the same type as the argument.

### Can I use abs() inside calc()?

Yes. It can appear anywhere a math value is allowed, like `calc(abs(var(--x)) * 2)`.

## Related cards

- [sign()](https://wonderium.dev/css/sign.md)
- [calc()](https://wonderium.dev/css/calc.md)
- [max()](https://wonderium.dev/css/max.md)
- [round()](https://wonderium.dev/css/round.md)
- [Math.abs()](https://wonderium.dev/js/math-abs.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/abs
- Last updated: 2026-11-25
