Function
Example
.main {
width: calc(100%-2rem);
}.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 arempart, so they respect zoom.
Interview questions
Can I use CSS variables inside calc()?
calc()?Yes, calc(var(--gap) * 2) works. The variable must hold a value that makes sense in the calculation.
Is rgba() still needed?
rgba() still needed?No, rgb() accepts an alpha value, like rgb(0 0 0 / 50%), so rgba() is a legacy alias.