calc()

Computes a value from a mathematical expression and lets you combine different units, for example percentages with pixels.

On this page

Example

#
CSS
.progress {
  height: 10px;
  border-radius: 999px;
  background: #27272a;
}

.progress-fill {
  width: calc(var(--progress) * 1%);
  height: 100%;
  border-radius: inherit;
  background: #10b981;
}
Preview

The page template puts just a number in the style attribute (--progress: 65), and calc() turns it into a width in percent.

Definition and usage

#

The calc() function lets you write an expression anywhere CSS expects a number, length, angle, time, or percentage. It supports addition +, subtraction -, multiplication *, division /, and parentheses. Its biggest advantage is mixing units: calc(100% - 80px) means “the full width of the parent minus 80 pixels”, and such a value can’t be computed in advance, because the parent’s width is known only in the browser.

Spaces are required around + and -. Without them the browser reads -80px as a negative number and treats the whole expression as invalid. Spaces around * and / are not required, but they improve readability. The simplest approach is to multiply and divide a length by a plain number: calc(10px * 3), calc(100% / 3). The result must fit the property, so calc(10px * 3px), which is pixels squared, won’t work in width.

The most powerful use is combining it with CSS variables. A variable can hold just a number, and calc() gives it a unit: calc(var(--progress) * 1%). You can also compute spacing from a single base value, for example calc(var(--gap) * 2). Functions can be nested, and an inner calc() can be replaced by plain parentheses.

When a value needs an upper or lower limit, use min(), max(), or clamp() instead of elaborate calculations. In them you also write expressions without the word calc, for example min(100% - 2rem, 60ch).

Syntax

#
Syntax
property: calc(<value> <operator> <value>);

operators: +  -  *  /

Common operations

#
  • calc(100% - 80px)

    parent width minus 80 px

    The tile takes the full width of the frame except the last 80 pixels. That gap stays fixed no matter how wide the frame is.
  • calc(100% / 3)

    one third of the parent

    Division by a number: the tile is exactly one third of the frame’s width.
  • calc(var(--n) * 40px)

    4 × 40px = 160px

    A number from a variable turned into a length. Change --n and the width recalculates by itself.
  • calc(1rem + 2vw)

    16px + 2% of the window width

    A text size made of a fixed part and a part that depends on the window. On wider screens the text grows, but never drops below 16 px.
  • calc(50% - 40px)

    centre of the parent

    The classic centring of an element of known width: the left margin is half the parent minus half the tile.
  • calc(360deg / 8)

    45deg

    You can also do arithmetic on angles and times. Here the rotation angle is one eighth of a full turn, which is 45 degrees.

Common operations in practice

#

calc(100% - 80px)

The tile takes the full width of the frame except the last 80 pixels. That gap stays fixed no matter how wide the frame is.
.demo { width: calc(100% - 80px); }

calc(100% / 3)

Division by a number: the tile is exactly one third of the frame’s width.
.demo { width: calc(100% / 3); }

calc(var(--n) * 40px)

A number from a variable turned into a length. Change --n and the width recalculates by itself.
.demo {
  --n: 4;
  width: calc(var(--n) * 40px);
}

calc(1rem + 2vw)

A text size made of a fixed part and a part that depends on the window. On wider screens the text grows, but never drops below 16 px.
.demo { font-size: calc(1rem + 2vw); }

calc(50% - 40px)

The classic centring of an element of known width: the left margin is half the parent minus half the tile.
.demo {
  width: 80px;
  margin-left: calc(50% - 40px);
}

calc(360deg / 8)

You can also do arithmetic on angles and times. Here the rotation angle is one eighth of a full turn, which is 45 degrees.
.demo {
  width: 80px;
  margin: 16px auto;
  rotate: calc(360deg / 8);
}

More examples

#
A full-window-width banner in a narrow column
CSS
.article {
  max-width: 280px;
  margin-inline: auto;
}

.full-bleed {
  margin-inline: calc(50% - 50vw);
}
Preview

The article column is 280 px wide, yet the banner still reaches the edges of the frame. The negative margin 50% - 50vw is exactly the distance from the column’s edge to the window’s edge.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Always put spaces around + and -. Writing calc(100%-20px) is invalid and the browser will drop the whole declaration.
  • You turn a number from a variable into a unit by multiplying, for example calc(var(--n) * 1px). Appending the unit, as in var(--n)px, won’t work.
  • To limit values, use clamp(), min(), and max() instead of building conditions in calc(). The code will be shorter and clearer.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.