Sub topic 3
Math in CSS: calc() and the rules for operators
This chapter assumes you know basic CSS syntax, common length units (px, rem, %, vw), and the box model. It covers calc(), the function that min(), max(), and clamp() build on.
Why calc() exists
Some values can't be known when you write the stylesheet. A sidebar might need to be "the container width minus 16rem", but the container's width is only known when the browser lays out the page. calc() lets you write that expression and defers the arithmetic until the browser can resolve it.
.main {
width: calc(100% - 16rem);
}The browser computes the percentage first, subtracts the rem value converted to pixels, and uses the single resulting length. You can mix units that CSS could not otherwise combine.
The four operators
calc() supports +, -, *, and /. Each has its own rules.
| Operator | Rule | Valid example | Invalid example |
|---|---|---|---|
| `+` and `-` | Whitespace is required on both sides. Both operands must have compatible types. | `calc(100% - 2rem)` | `calc(100%-2rem)` |
| `*` | At least one operand must be a plain number. | `calc(2rem * 3)` | `calc(2rem * 3rem)` |
| `/` | The right operand must be a plain, non-zero number. | `calc(100% / 3)` | `calc(100% / 2rem)` |
Two points explain most errors:
- Without spaces,
100%-2remis not read as subtraction, because-2remlooks like a single negative value. The whole declaration becomes invalid. - Multiplying two lengths would give an area, not a length. CSS has no such type, so the browser rejects it.
You must also keep types compatible when adding or subtracting. A length plus a bare number is invalid, even when the number is 0.
.bad { margin-left: calc(1rem + 10); } /* invalid: length + number */
.good { margin-left: calc(1rem + 10px); }Nesting and parentheses
Parentheses group operations, and standard precedence applies: multiplication and division run before addition and subtraction. A bare parenthesized group inside calc() behaves like a nested calc().
.card {
/* Three equal columns with 1rem gaps between them */
width: calc((100% - 2 * 1rem) / 3);
}Here 2 * 1rem is a number times a length, so it is valid. Subtracting it from 100% is valid, and dividing by the number 3 is valid.
Math inside other functions
Inside min(), max(), and clamp(), each argument is already a math expression. You can write operators there without wrapping them in calc().
.container {
width: min(100% - 2rem, 70rem);
padding-inline: max(1rem, 5vw - 1rem);
}Use calc() only where you need math on its own, such as width: calc(100% - 2rem).
Negating and reusing values
Negating a value works through multiplication, since a leading minus sign on a function is not allowed. Custom properties work well in these expressions.
:root {
--gutter: 1.5rem;
}
.bleed {
margin-inline: calc(-1 * var(--gutter));
}If you wrote -var(--gutter), the declaration would be invalid.
Where percentages resolve
A percentage inside calc() refers to whatever the property normally uses as its reference. For width it is the containing block's width. For font-size it is the parent's font size. For vertical padding it is still the containing block's width. Check the property's reference before relying on %.
Failure behavior
An invalid calc() does not throw an error. The browser drops that declaration and falls back to an earlier one or to the default. If a width silently has no effect, check your spaces and operand types first.
Exercise
Why is width: calc(50%+2rem) ignored, and how do you fix it? Also, why does calc(2rem * 3rem) fail?
Show answer
The + has no whitespace around it, so the parser doesn't read it as addition and the declaration is invalid. Write calc(50% + 2rem). The second expression multiplies two lengths, but * requires at least one plain number, so use something like calc(2rem * 3).