CSS · Layout and visual systems

How does calc() work and when should you use it?

Hardcalccss functionsunitsUpdated Oct 2026
Quiz this track

Short answer

calc() performs arithmetic in CSS, mixing compatible units and resolving at used-value time. Addition and subtraction require spaces around the operators, and division must be by a number.

calc lets you combine lengths, percentages, viewport units and custom properties in one expression. It is evaluated by the browser after relative units resolve, so it can express relationships that would otherwise need JavaScript or magic numbers.

css
.sidebar {
  width: calc(100% - 16rem);
  padding: calc(1rem + 2vw);
}

.full {
  min-height: calc(100svh - 4rem);
}

Rules and limits

  • + and - must have whitespace on both sides
  • * and / do not require spaces, but division must be by a unitless number
  • You can nest min(), max() and clamp() inside calc
  • Custom properties can hold expressions, but malformed substitution invalidates the declaration
  • Division by zero makes the expression invalid rather than returning Infinity
  • Compatible units are required; you cannot add a length to a color

A common use is subtracting a fixed header from a dynamic viewport height. Another is mixing a percentage with a gap width. If the layout itself can express the relationship, such as flex or grid with gap, prefer the layout system over calculating widths manually.

calc is not a performance problem in normal use, but heavy recalculation of complex expressions during animation can be. Animate transform and opacity rather than recalculating layout geometry every frame.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Why do plus and minus need spaces in calc?
  2. 2Can calc divide by a unit value?
  3. 3How does calc interact with custom properties?
  4. 4When should you use flex or grid instead of calc?

Say it in the interview

calc performs arithmetic in CSS and resolves at used-value time, so I can mix percentages, viewport units, rem and custom properties. Addition and subtraction need whitespace, and division has to be by a number. I use it for things like subtracting a header from the viewport height, but if flex or grid can express the layout directly, I prefer those because there is less manual math to maintain.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live CSS mock interview and get scored feedback on depth, clarity and confidence.