Sub topic 3
Math in CSS: calc() and the Rules for Operators
This chapter assumes you know basic CSS syntax, the common length units (px, rem, em, %, vw), and the width property. Later chapters build min(), max(), and clamp() on the rules covered here. Those functions use the same operators and the same unit rules as calc().
Why CSS needs math
Plain CSS can say "50% wide" or "2rem wide". It cannot say "everything except a 2rem gutter" without math. Units like % are only resolved once the browser knows the layout, so you can't precompute the answer yourself. The calc() function lets the browser do the arithmetic at that point.
.panel {
width: calc(100% - 2rem);
}If the parent is 800px wide and the root font size is 16px, the browser computes 800px - 32px and uses 768px. If the parent resizes, the result updates with it. You never write 768px, and that is what makes the layout flexible.
The four operators
calc() supports addition +, subtraction -, multiplication *, and division /. Multiplication and division bind tighter than addition and subtraction, as in school math. Parentheses change the order.
.example {
/* 1rem + (2 * 0.5rem) = 2rem */
margin-inline: calc(1rem + 2 * 0.5rem);
/* (1rem + 2) would be invalid, but this multiplies first, then adds */
padding-inline: calc((1rem + 1rem) * 2);
}Plain parentheses inside calc() behave like a nested calc(). You can nest as deeply as you need, but deep nesting is hard to read, so introduce custom properties when an expression grows.
How the browser evaluates it
The whitespace rule
The + and - operators must have whitespace on both sides. The reason is tokenization. In 100%-2rem, the parser reads -2rem as a negative length, so it sees two values with no operator between them. The declaration is dropped.
.broken { width: calc(100%-2rem); } /* invalid: no spaces around - */
.works { width: calc(100% - 2rem); } /* valid */The * and / operators do not need spaces. Still, write them with spaces for consistency, so you never have to remember which operators are picky.
The type rules
Each operator restricts what it accepts. The restrictions are what trip people up.
- Addition and subtraction need operands of compatible types. You can add a length to a length, or a length to a percentage. You cannot add a plain number to a length.
- Multiplication needs at least one side to be a plain number (no unit).
- Division needs the right side to be a plain number.
So calc(2rem * 3) works, because 3 is a number. calc(2rem * 3rem) fails, because a "rem squared" value does not exist in layout. In the same way, calc(100% / 2rem) fails because the divisor has a unit.
| Expression | Valid? | Reason |
|---|---|---|
| `calc(100% - 2rem)` | Yes | A percentage and a length can be subtracted. |
| `calc(100%-2rem)` | No | `-` needs whitespace on both sides. |
| `calc(2rem * 3)` | Yes | One side is a plain number. |
| `calc(2rem * 3rem)` | No | Neither side is a plain number. |
| `calc(100% / 4)` | Yes | The divisor is a plain number. |
| `calc(100% / 2rem)` | No | The divisor has a unit. |
| `calc(1rem + 4)` | No | A number cannot be added to a length. |
| `calc(1rem + 0)` | No | A bare `0` is a number inside `calc()`, not a length. |
| `calc(1.2 + 0.1)` | Yes (for `line-height`) | Both are numbers, and `line-height` accepts a number. |
Dividing by zero also makes the expression invalid or produces an extreme value, depending on the context. Avoid any divisor that could be zero, including one that comes from a custom property.
Mixing percentages and fixed units
The most useful trick in calc() is mixing a percentage with a fixed unit. A percentage depends on the context, and a fixed unit does not. The percentage's reference depends on the property:
- For
width, a percentage refers to the width of the containing block. - For
font-size, a percentage refers to the parent's font size. - For
paddingandmargin, a percentage refers to the width of the containing block, even for vertical sides.
A fixed-width sidebar with a flexible main column is the classic case.
.layout {
display: flex;
}
.sidebar {
width: 15rem;
}
.main {
width: calc(100% - 15rem);
}The .main element takes whatever the sidebar leaves. In a 1000px container with a 16px root, that is 1000px - 240px = 760px.
Equal columns with gaps
You can combine all four operators to size columns exactly. Suppose you want three columns and two 1rem gaps inside the container.
.row {
display: flex;
gap: 1rem;
}
.row > .col {
/* subtract both gaps, then split what is left three ways */
width: calc((100% - 2 * 1rem) / 3);
}In an 800px container, the calculation is (800px - 32px) / 3 = 256px per column. Three columns of 256px plus two gaps of 16px fill exactly 800px. Note the parentheses. Without them, / would bind first and the subtraction would apply to only one third of the width.
calc() with custom properties
Custom properties are substituted into the expression before it is parsed. That means you can store a unit-bearing value or a plain number and reuse it.
:root {
--gutter: 1rem;
--columns: 4;
}
.grid-item {
width: calc((100% - (var(--columns) - 1) * var(--gutter)) / var(--columns));
}Here --columns is a plain number, so (var(--columns) - 1) is number arithmetic. Multiplying that number by --gutter gives a length, which is valid. In a 1000px container with a 16px root, the width is (1000px - 3 * 16px) / 4 = 238px.
A plain number becomes a length by multiplying it by a unit:
:root {
--size: 3;
}
.icon {
/* 3 * 1rem = 3rem */
width: calc(var(--size) * 1rem);
}calc() outside lengths
calc() is not limited to lengths. It works wherever the property accepts the resulting type: numbers, integers, angles, times, and percentages.
.badge {
/* numbers: z-index accepts an integer */
z-index: calc(var(--layer, 1) + 1);
/* angles */
rotate: calc(360deg / 8);
/* times */
transition-duration: calc(200ms * 1.5);
}The same type rules apply to each. calc(360deg / 8) is valid because the divisor is a plain number.
Math without calc()
Inside min(), max(), and clamp(), each argument is already a math expression. You can write min(100% - 2rem, 70rem) without wrapping the first argument in calc(). The operators and whitespace rules are the same. Outside those functions, you still need calc(). The next chapters use this heavily.
Common mistakes
- Leaving out the spaces around
+or-. - Multiplying two lengths, or dividing by a length.
- Adding a bare number to a length.
- Dividing by a value that can be zero.
- Forgetting that percentage references differ by property, so
calc(50% + 1rem)means different things onwidthand onfont-size.
Exercise
A container is 1000px wide and the root font size is 16px. What width does calc((100% - 3 * 1rem) / 4) give a child element, and why is the calculation valid?
Show answer
3 * 1rem is 48px, so the numerator is 1000px - 48px = 952px. Dividing by 4 gives 238px. It is valid because the multiplication has a plain number (3) on one side, the subtraction combines a percentage with a length, and the divisor (4) is a plain number.
Exercise
Why does width: calc(100%-2rem); fail, and how do you fix it? Then explain why calc(1rem + 2) is also invalid.
Show answer
Without whitespace around the minus sign, the parser reads -2rem as a negative length next to 100%, so no operator exists and the declaration is dropped. Write calc(100% - 2rem). The second expression is invalid because a plain number (2) cannot be added to a length. Use calc(1rem + 2rem) or multiply a number by a unit, as in calc(1rem + 2 * 1rem).