# Function

> The Function in CSS, such as `calc()` or `rgba()`, performs calculations or manipulations on values.

## What are CSS functions like calc() and var()?

The special commands that take one or more arguments and return a value used in a style rule. Examples include `calc()` for calculations, `url()` for linking resources and `rgb()` for color definitions. Functions provide dynamic and flexible ways to define styles.

- Topic: Basics
- Type: concept, syntax

## Example

Wrong:

```css
.main {
  width: calc(100%-2rem);
}
```

Right:

```css
.main {
  width: calc(100% - 2rem);
}
```

Without spaces `-2rem` is read as a negative number, not a subtraction, so the value is invalid.

## Common mistakes

- Writing `calc(100%-20px)` without spaces, so the whole value is invalid.
- Adding a space between the function name and the parenthesis.

## Key points

- Operators in `calc()` have spaces around `+` and `-`.
- Mixed units are combined with `calc()`, not written as plain math.
- Fluid sizes use `clamp()` with a `rem` part, so they respect zoom.

## Interview questions

### Can I use CSS variables inside `calc()`?

Yes, `calc(var(--gap) * 2)` works. The variable must hold a value that makes sense in the calculation.

### Is `rgba()` still needed?

No, `rgb()` accepts an alpha value, like `rgb(0 0 0 / 50%)`, so `rgba()` is a legacy alias.

## Related cards

- [calc()](https://wonderium.dev/css/calc.md)
- [var()](https://wonderium.dev/css/var.md)
- [clamp()](https://wonderium.dev/css/clamp.md)
- [min()](https://wonderium.dev/css/min.md)
- [rgb() / rgba()](https://wonderium.dev/css/rgb.md)

---

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