CSS calc(): How to Do Math in CSS (With Examples)

By Marko Denic
Contents
  1. calc() syntax
  2. The four operators
  3. Spaces around + and - are required
  4. Multiply and divide by plain numbers
  5. Mixing units
  6. Practical calc() examples
  7. Full height minus the header
  8. Equal columns with gaps
  9. Center an absolutely positioned element
  10. Fluid font size
  11. Offset an anchor below a sticky header
  12. calc() with CSS variables
  13. Build a spacing scale from one variable
  14. Turn a unitless number into a length
  15. Nesting calc()
  16. calc() vs min(), max() and clamp()
  17. Fluid type with clamp()
  18. Common calc() mistakes
  19. Browser support
  20. FAQ

The CSS calc() function lets you do math right in your stylesheet. It adds, subtracts, multiplies and divides values, and it can mix units that CSS can’t otherwise combine, like a percentage and pixels: width: calc(100% - 120px).

The browser works out the result when it lays out the page, so 100% means whatever 100% is at that moment. That’s what makes calc() useful: you describe the relationship, and the layout stays correct at every screen size.

Here are three things calc() does in the demo below: a sidebar layout, equal columns with gaps, and a centered box.

Demo:

100% - 120px
1/3
1/3
1/3
centered

calc() syntax

The four operators

Inside calc() you can use +, -, * and /, and group parts with parentheses. The result works anywhere CSS expects a length, number, percentage, angle or time: width, margin, font-size, top, transform, transition-duration and so on.

css
.box {
  width: calc(100% - 40px);       /* subtract */
  margin-top: calc(1rem + 4px);   /* add */
  padding: calc(8px * 2);         /* multiply */
  height: calc(100vh / 3);        /* divide */
  font-size: calc((1rem + 2px) * 1.25); /* group with parentheses */
}

Spaces around + and - are required

This is the most common reason calc() “doesn’t work”. Without spaces, the browser reads -120px as a negative number glued to the previous value, the expression is invalid, and the whole declaration is ignored. * and / don’t need spaces, but adding them keeps things readable.

css
/* Works */
.main { width: calc(100% - 120px); }

/* Ignored: no spaces around the minus */
.main { width: calc(100%-120px); }

/* Ignored: same problem with plus */
.main { width: calc(100%+120px); }

Multiply and divide by plain numbers

With *, at least one side must be a plain number. With /, the right side must be a plain number. Dividing by zero makes the declaration invalid.

css
/* Works */
width: calc(100% / 3);
width: calc(3 * 2rem);

/* Invalid */
width: calc(2rem * 2rem);  /* length × length */
width: calc(100% / 0);     /* division by zero */

Mixing units

This is what calc() is really for. Percentages, px, rem, em and viewport units can all be combined, as long as the result makes sense for the property.

css
/* Full width minus padding on both sides */
.full-bleed {
  width: calc(100% - 2 * 1.5rem);
}

/* Viewport height minus a fixed amount */
.panel {
  max-height: calc(100vh - 8rem);
}

Practical calc() examples

These are the patterns you’ll actually reach for in real projects.

Full height minus the header

A layout that fills the screen under a fixed header. Use 100dvh instead of 100vh: on mobile, vh ignores the browser’s address bar, so the content ends up slightly taller than the screen.

css
.header {
  height: 64px;
}

.content {
  min-height: calc(100dvh - 64px);
}

Equal columns with gaps

Three columns with two 16px gaps between them: take the gaps out of the full width, then divide what’s left by three. In CSS Grid, 1fr does this for you; calc() is what you need with Flexbox, inline-block elements or widths set on the items themselves.

css
.row {
  display: flex;
  gap: 16px;
}

.row > * {
  width: calc((100% - 2 * 16px) / 3);
}

Center an absolutely positioned element

If you know the element’s size, you can center it with left and top alone: 50% of the parent minus half of the element. Need a refresher on how absolute works? See position absolute vs relative. For elements of unknown size, the Flexbox and Grid centering tricks are simpler.

css
.parent {
  position: relative;
}

.modal {
  position: absolute;
  width: 400px;
  height: 200px;
  left: calc(50% - 200px); /* half the width */
  top: calc(50% - 100px);  /* half the height */
}

Fluid font size

Add a viewport unit to a fixed size, and the text grows smoothly with the screen instead of jumping at breakpoints. The 1rem part keeps it readable on small screens and respects the user’s font settings. Use clamp() (below) to cap how big it gets.

css
h1 {
  font-size: calc(1.5rem + 2vw);
}

p {
  font-size: calc(1rem + 0.25vw);
}

Offset an anchor below a sticky header

When you link to a heading on the same page, a sticky header can cover it. scroll-margin-top with calc() leaves room for the header plus some breathing space.

css
h2[id] {
  scroll-margin-top: calc(64px + 1rem);
}

calc() with CSS variables

calc() and custom properties are a great pair: store a value once, derive everything else from it, and change one variable to update the whole layout. The same idea powers a seasonal website theme: override a handful of color variables and the whole site changes palette.

Build a spacing scale from one variable

Change --space and every margin and padding that uses it follows.

css
:root {
  --space: 8px;
  --header-height: 64px;
}

.card {
  padding: calc(var(--space) * 2);   /* 16px */
  margin-bottom: calc(var(--space) * 3); /* 24px */
}

.content {
  min-height: calc(100dvh - var(--header-height));
}

Turn a unitless number into a length

Store a plain number in a variable and multiply it by 1px (or 1rem, 1deg…) to give it a unit. This is handy when JavaScript sets the variable, or when the same number drives several properties.

css
.progress {
  --value: 65; /* set from JS: el.style.setProperty('--value', 65) */
  width: calc(var(--value) * 1%);
}

.spinner {
  --turns: 3;
  rotate: calc(var(--turns) * 360deg);
}

Nesting calc()

You can put calc() inside calc(), which happens naturally when a variable itself contains one. Plain parentheses work the same, so you never need to write a nested calc() by hand.

css
:root {
  --sidebar: calc(200px + 2rem);
}

.main {
  /* Becomes calc(100% - calc(200px + 2rem)), which is fine */
  width: calc(100% - var(--sidebar));
}

calc() vs min(), max() and clamp()

These three are newer math functions that pick a value instead of computing one:

  • min(a, b) uses the smaller value, so it works like a maximum size: width: min(100%, 600px).
  • max(a, b) uses the larger value, so it works like a minimum size: font-size: max(1rem, 2vw).
  • clamp(min, preferred, max) keeps a value between two limits. It’s the best tool for fluid type.

All three accept math directly, so you don’t need calc() inside them: clamp(1rem, 0.8rem + 1vw, 1.5rem) works as is.

Fluid type with clamp()

The calc-style expression in the middle grows with the viewport, and the two outer values stop it from getting too small or too big.

css
h1 {
  /* At least 2rem, at most 3.5rem, fluid in between */
  font-size: clamp(2rem, 1.5rem + 2vw, 3.5rem);
}

.container {
  /* Full width with 1rem side gutters, never wider than 1100px */
  width: min(100% - 2rem, 1100px);
  margin-inline: auto;
}

Common calc() mistakes

  • Missing spaces around + or -. calc(100%-20px) is invalid and silently ignored.
  • A unitless zero in a sum. calc(0 + 20px) is invalid because 0 is a number, not a length. Write 0px.
  • Multiplying two lengths. calc(2rem * 2rem) is invalid; one side of * must be a plain number.
  • Dividing by a length. calc(100% / 20px) isn’t supported reliably across browsers yet. Divide by a plain number.
  • Letting Sass do the math. In Sass, 100% - 20px throws an “incompatible units” error, because Sass can’t know what 100% will be. Wrap it in calc() and the browser does it instead.

To debug, open DevTools: an invalid calc() shows the declaration struck through with a warning icon. The Computed tab shows the final value the browser worked out.

Browser support

calc() works in every browser in use today, and has for over a decade. Using CSS variables inside it works everywhere custom properties do. min(), max() and clamp() have been supported in all major browsers since 2020.

FAQ

What does calc() do in CSS?

It calculates a value from an expression, like calc(100% - 120px). Its main job is mixing units that CSS can’t otherwise combine, such as a percentage and pixels, and the browser works out the result at layout time.

Why is my calc() not working?

Usually missing spaces: + and - must have a space on both sides. Other causes are a unitless 0 in a sum, multiplying two lengths, or dividing by a length. Invalid calc() is ignored, so the property falls back to its previous value.

Can I use CSS variables in calc()?

Yes: calc(var(--space) * 2). To turn a unitless variable into a length, multiply it by one unit: calc(var(--n) * 1px).

Is calc() bad for performance?

No. The browser evaluates it during layout, like any other value, and the cost is negligible.

What’s the difference between calc() and clamp()?

calc() computes one value. clamp(min, preferred, max) keeps a value between two limits, and accepts math directly, so you don’t need calc() inside it.