Where calc() is allowed
Values are the only place calc() can appear. It can supply a whole property value, form part of one, or sit inside another function that makes up a property β for example within the color stops of a gradient.
.el {
font-size: calc(3vw + 2px);
width: calc(100% - 20px);
height: calc(100vh - 20px);
padding: calc(1vw + 5px);
}
Partial use looks like this:
.el {
margin: 10px calc(2vw + 5px);
border-radius: 15px calc(15px / 3) 4px 2px;
transition: transform calc(1s - 120ms);
}
And nested inside other functions:
.el {
background: #1E88E5 linear-gradient(
to bottom,
#1E88E5,
#1E88E5 calc(50% - 10px),
#3949AB calc(50% + 10px),
#3949AB
);
}
What you can calculate
All of this is numeric math β no strings. CSS lengths of every kind are fair game: px, %, em, rem, in, mm, cm, pt, pc, ex, ch, vh, vw, vmin, vmax. Unit-less numbers work too, as in line-height: calc(1.2 * 1.2);, as do angles such as transform: rotate(calc(10deg * 5));. A calc() that performs no calculation at all is still valid:
.el {
/* Little weird but OK */
width: calc(20px);
}
Media queries are the exception: applied correctly, with length units as a property value, calc() still won't work there.
@media (max-width: 40rem) {
/* Narrower or exactly 40rem */
}
/* Nope! */
@media (min-width: calc(40rem + 1px)) {
/* Wider than 40rem */
}
The operators and their rules
calc() gives you +, -, * and /, but each carries its own requirement.
Addition and subtraction
Both operands must be lengths. An invalid value invalidates the entire individual declaration.
.el {
/* Valid π */
margin: calc(10px + 10px);
/* Invalid π */
margin: calc(10px + 5);
}
Division
The second number must be unit-less.
.el {
/* Valid π */
margin: calc(30px / 3);
/* Invalid π */
margin: calc(30px / 10px);
/* Invalid π (can't divide by 0) */
margin: calc(30px / 0);
}
Multiplication
One of the two numbers must be unit-less.
.el {
/* Valid π */
margin: calc(10px * 3);
/* Valid π */
margin: calc(3 * 10px);
/* Invalid π */
margin: calc(30px * 3px);
}
Whitespace
Spacing is mandatory around addition and subtraction only. Per Tab Atkins, the reason is parsing: 2px-3px gets read as the number "2" and the unit "px-3px", and the + can be "consumed by the number syntax." It is not about the -- syntax of custom properties.
.el {
/* Valid π */
font-size: calc(3vw + 2px);
/* Invalid π */
font-size: calc(3vw+2px);
/* Valid π */
font-size: calc(3vw - 2px);
/* Invalid π */
font-size: calc(3vw-2px);
}
Negative numbers are fine (calc(5vw - -5px)), and that case shows why the whitespace isn't just required but useful. Multiplication and division don't need surrounding space, though including it is good general advice. Space on the outside doesn't matter, and line breaks are allowed:
.el {
/* Valid π */
width: calc(
100% / 3
);
}
One thing to watch: no space between calc() and its opening paren.
.el {
/* Invalid π */
width: calc (100% / 3);
}
Parentheses and nesting
calc(calc()) works but is never necessary β it behaves exactly like an extra set of parens without the inner calc().
.el {
width: calc(
calc(100% / 3)
-
calc(1rem * 2)
);
}
Inside calc() the parens alone do the job:
.el {
width: calc(
(100% / 3)
-
(1rem * 2)
);
}
In that case the normal order of operations already handles it: multiplication and division run before addition and subtraction, so no parens are needed at all:
.el {
width: calc(100% / 3 - 1rem * 2);
}
Use parens freely for clarity, and you will need them when the order of operations doesn't work in your favor β say, when the addition or subtraction must happen first.
.el {
/* This */
width: calc(100% + 2rem / 2);
/* Is very different from this */
width: calc((100% + 2rem) / 2);
}
Preprocessor math compared
Sass has math built in, so there is a little overlap:
$padding: 1rem;
.el[data-padding="extra"] {
padding: $padding + 2rem; // processes to 3rem;
margin-bottom: $padding * 2; // processes to 2rem;
}
Same-unit addition and multiplication by unit-less numbers both work there, but mixing units doesn't, and the constraints resemble calc()'s (multiplication and division must involve unit-less numbers). More importantly, the most valuable calc() cases can't be preprocessed. Take:
/* Percentage units being mixed with pixel units */
width: calc(100% - 20px);
That means "as wide as the element is, minus 20 pixels." In a fluid-width situation there is no way to pre-compute that in pixels alone, so it has to happen in the browser at runtime. Sass can't polyfill it.
Other mixed-unit examples:
transform: rotate(calc(1turn + 45deg));
animation-delay: calc(1s + 15ms);
Those could be preprocessed, since they don't reference anything resolved at runtime.
Even where calc() isn't strictly required, it can "show your work" in the CSS. To take exactly one-seventh of an element's width:
.el {
/* This is easier to understand */
width: calc(100% / 7);
/* Than this is */
width: 14.2857142857%;
}
Which might surface through a self-built CSS API:
[data-columns="7"] .col { width: calc(100% / 7); }
[data-columns="6"] .col { width: calc(100% / 6); }
[data-columns="5"] .col { width: calc(100% / 5); }
[data-columns="4"] .col { width: calc(100% / 4); }
[data-columns="3"] .col { width: calc(100% / 3); }
[data-columns="2"] .col { width: calc(100% / 2); }
Custom properties
Custom properties hold values you then use in a calculation:
html {
--spacing: 10px;
}
.module {
padding: calc(var(--spacing) * 2);
}
Picture a setup with heavy configuration at the top of the stylesheet and the rest of the CSS consuming those properties. Custom properties can also reference one another, with math done first (no calc() yet) and applied later:
html {
--spacing: 10px;
--spacing-L: var(--spacing) * 2;
--spacing-XL: var(--spacing) * 3;
}
.module[data-spacing="XL"] {
padding: calc(var(--spacing-XL));
}
That forces you to remember the calc() at the point of use; it trades some convenience for readability. Custom properties can also arrive from the HTML β Splitting.js adds indexes to words and characters this way.
<div style="--index: 1;"> ... </div>
<div style="--index: 2;"> ... </div>
<div style="--index: 3;"> ... </div>
div {
/* Index value comes from the HTML (with a fallback) */
animation-delay: calc(var(--index, 1) * 0.2s);
}
Deferring units
If storing numbers unit-less is easier, add the unit at application time by multiplying by 1 and the unit:
html {
--importantNumber: 2;
}
.el {
/* Number stays 2, but it has a unit now */
padding: calc(var(--importantNumber) * 1rem);
}
Colors
RGB and HSL values contain numbers you can manipulate, letting you define base HSL values and derive a whole system from them:
html {
--H: 100;
--S: 100%;
--L: 50%;
}
.el {
background: hsl(
calc(var(--H) + 20),
calc(var(--S) - 10%),
calc(var(--L) + 30%)
)
}
No attr()
attr() looks tempting for pulling numbers out of HTML, but there are no types in play β it is for strings used with content only:
div::before {
content: attr(data-color);
}
Pulling a number in for arithmetic won't work:
<div class="grid">...</div>
.grid {
display: grid;
/* Neither of these work */
grid-template-columns: repeat(attr(data-columns), 1fr);
grid-gap: calc(1rem * attr(data-gap));
}
That costs little, since custom properties in the HTML are just as useful:
<div class="grid" style="--columns: 7; --gap: 2rem;">...</div>
.grid {
display: grid;
/* Yep! */
grid-template-columns: repeat(var(--columns), 1fr);
grid-gap: calc(var(--gap));
}
Tooling and support
DevTools report calc() as authored in the stylesheet:

For the computed value, use the Computed tab present in every browser's DevTools.
| Chrome | Firefox | IE | Edge | Safari |
|---|---|---|---|---|
| 19* | 4* | 11 | 12 | 6* |
Mobile / Tablet
| Android Chrome | Android Firefox | Android | iOS Safari |
|---|---|---|---|
| 157 | 160 | 157 | 6.0-6.1* |
For very old targets like IE 8 or Firefox 3.6, the usual trick is declaring another property or value ahead of the calc() one:
.el {
width: 92%; /* Fallback */
width: calc(100% - 2rem);
}
Known issues are all tied to old browsers (Can I use lists 13). Examples: Firefox <59 doesn't support calc() in color functions, e.g. color: hsl(calc(60 * 2), 100%, 50%); IE 9β11 won't render box-shadow if calc() is used in any value; and neither IE 9β11 nor Edge supports width: calc() on table cells.
How developers put it to work
I used it to create a full-bleed utility class:
.full-bleed { width: 100vw; margin-left: calc(50% - 50vw); }I'd saycalc()is in my top 3 CSS things.
I used it to make space for a sticky footer.
I used it to set some fluid type / dynamic typography⦠a calculated
font-sizebased on minimums, maxiums, and a rate of change from viewport units. Not just thefont-size, butline-heighttoo.
When fluid type involves viewport units like this, include a rem or em unit somewhere so users can still zoom the font up or down.
One I really like is having a "content width" custom property and then using that to create the spacing that I need, like margins:
.margin { width: calc( (100vw - var(--content-width)) / 2); }
I used it to create a cross-browser drop-cap component. Here's a part of it:
.drop-cap { --drop-cap-lines: 3; font-size: calc(1em * var(--drop-cap-lines) * var(--body-line-height)); }
I used it to make some images overflow their container on an article page.
I used it to place a visualization correctly on the page by combining it with padding and
vw/vhunits.
I use it to overcome limitations in
background-position, but expecially limitations in positioning color stops in gradients. Like "stop0.75emshort of the bottom".
Further tricks
- A two-up grid that breaks into a single column with no media query
- An aspect-ratio-ish hero component
- Enforcing high-contrast colors
- Help with the coordinates of a percentage-based
clip-path



