calc()
Computes a value from a mathematical expression and lets you combine different units, for example percentages with pixels.
Example
#.progress {
height: 10px;
border-radius: 999px;
background: #27272a;
}
.progress-fill {
width: calc(var(--progress) * 1%);
height: 100%;
border-radius: inherit;
background: #10b981;
}<p style="margin: 0 0 8px;">Module 2: Flexbox, 65% completed</p>
<div class="progress"><div class="progress-fill" style="--progress: 65"></div></div>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
#property: calc(<value> <operator> <value>);
operators: + - * /Common operations
#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.one third of the parent
Division by a number: the tile is exactly one third of the frame’s width.4 × 40px = 160px
A number from a variable turned into a length. Change--nand the width recalculates by itself.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.centre of the parent
The classic centring of an element of known width: the left margin is half the parent minus half the tile.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)
.demo { width: calc(100% - 80px); }calc(100% / 3)
.demo { width: calc(100% / 3); }calc(var(--n) * 40px)
--n and the width recalculates by itself..demo {
--n: 4;
width: calc(var(--n) * 40px);
}calc(1rem + 2vw)
.demo { font-size: calc(1rem + 2vw); }calc(50% - 40px)
.demo {
width: 80px;
margin-left: calc(50% - 40px);
}calc(360deg / 8)
.demo {
width: 80px;
margin: 16px auto;
rotate: calc(360deg / 8);
}More examples
#.article {
max-width: 280px;
margin-inline: auto;
}
.full-bleed {
margin-inline: calc(50% - 50vw);
}<div class="article">
<p style="margin: 0 0 10px;">The article has a narrow column so it is comfortable to read.</p>
<div class="full-bleed" style="padding: 14px; background: linear-gradient(90deg, #7c3aed, #0ea5e9); text-align: center; font-weight: 700;">Full-width banner</div>
<p style="margin: 10px 0 0;">After it the text goes back to the column.</p>
</div>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-. Writingcalc(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 invar(--n)px, won’t work. - To limit values, use
clamp(),min(), andmax()instead of building conditions incalc(). The code will be shorter and clearer.
Related entries
#- clamp()Keeps a value between a minimum and a maximum, and lets it grow smoothly in between. The basis of fluid typography.
- min()Picks the smallest of the given values. It works as an upper limit, for example a width of 100% of the parent, but no more than 600px.
- max()Picks the largest of the given values. It works as a lower limit, for example spacing of 5% of the width, but never less than 16px.
- var()Inserts the value of a CSS variable, that is, a custom property written as --name. It also lets you provide a fallback value.
- UnitsAn overview of length units in CSS: from fixed pixels, through font-relative em and rem, to viewport, container, and grid units.
- widthSets the width of an element.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.