CSS calc()
- type
- function
- applies to
- any property that takes a length, percentage, or number
- inherits
- n/a
- common values
- calc(100% - 240px), calc(100vh - 64px), calc(1rem + 2vw)
Where it goes
Anywhere a length goes: width, height, padding, margin, font-size, top, left. Write the math inside the parentheses.
Example
.main {
width: calc(100% - 240px);
min-height: calc(100vh - 64px);
}Common mistakes
- Leaving out the spaces around + and -. calc(100%-240px) is invalid and the whole declaration is dropped. The spaces are required.
- Using it where a simpler tool exists. calc(100% - 32px) for padding is what box-sizing: border-box already solves. calc for equal columns is what fr already solves.
- Dividing by a value with a unit. calc(100% / 50px) is not allowed. The number you divide by must be plain.