How do I use a CSS calc function inside another CSS calc function? According to this post it is possible but there is no example of that.
3 Answers
It is possible to use calc() inside another calc().
An example:
div{
width: calc(100% - (1% + 30px));/* calc(1% + 30px) is nested inside calc()*/
}
div p{
width: calc(100% - 30px);/*100% is total width of the div*/
}
Update on nested calc with css variables:
.foo {
--widthA: 100px;
--widthB: calc(var(--widthA) / 2);
--widthC: calc(var(--widthB) / 2);
width: var(--widthC);
}
After all variables are expanded, widthC's value will be calc( calc( 100px / 2) / 2), then when it's assigned to .foo's width property, all inner calc()s (no matter how deeply nested) will be flattened to just parentheses, so the width property's value will be eventually calc( ( 100px / 2) / 2), i.e. 25px. In short: a calc() inside of a calc() is identical to just parentheses.
So, the current spec as well proves this using parentheses inside calc() is nested calc.
Learn more about css variables here.

- 83,432
- 35
- 168
- 231
-
1Thats for the reply . I see you are using just one calc function. My question was how to use one calc Inside another calc function. – Raghavendra Prasad Dec 22 '14 at 05:44
-
Basically you can not use calc inside a calc, so Bhojendra Rauniyar's solution is do calc separately and save it in css variable. Finally you can get what you want. – bnqtoan Aug 23 '23 at 08:29
I'm one of the editors of the Values & Units spec, which defines calc().
calc() should be nestable inside of calc(). There was an oversight in the grammar/definitions for a while that technically didn't allow it, but I recently fixed that. Implementations haven't caught up yet, to my knowledge (tho it looks like Chrome 51 and Firefox 48 will have this fixed, woo!).
That said, there is zero reason to nest calc() expressions normally - they're exactly identical to just using parentheses. The only reason to do it at all is when using custom properties/variables.

- 18,065
- 2
- 31
- 32
-
Hey Tab! Do note that about the time you wrote this, the fix for this landed in Blink. It was also fixed in Gecko before that. I guess you were involved with both fixes, anyway thanks for that, I needed it (for custom properties ;)). So fix will be in Chromium 51, Opera 38 and Firefox 48. – odinho - Velmont Apr 06 '16 at 13:06
-
-
2Here's a use case, although you might not like it. I'm using styled components, and it's convenient to define some border width based off a value in a table somewhere. I have another property based off this property, and another off this one. So I have `w === '2px'` (say), and `w_ === \`calc(${w} * 2)\``, and `w__ === \`calc(${w_} * 2\``. This is somewhat more contrived than the actual code, but it's the basic idea. – Nathan Chappell Sep 10 '20 at 12:29
-
Here's an example that would require a nested calc(): `width: calc(100vw - calc(var(--page-margin) * 2))` – Alain Duchesneau Feb 09 '22 at 22:17
-
That does not require nested calc, just parentheses: `calc(100vw - (var(--page-margin) * 2))`. – Xanthir Sep 27 '22 at 17:56
The reference you quoted is admittedly a bit confusing.
It is not possible to use a calc
function inside another calc
.
From the specs here: http://dev.w3.org/csswg/css-values/#calc-notation
...Components of a calc() expression can be literal values, attr() or calc() expressions, or values..
You can have calc
expression inside the expressions, but not the calc()
function itself.
And an example is given in that ref for nested expressions:
width: calc(100%/3 - 2*1em - 2*1px);
And also for multiple calc
for multiple properties:
margin: calc(1rem - 2px) calc(1rem - 1px);
The syntax from the spec above:
The syntax of a calc() function is:
<calc()> = calc( <calc-sum> ) <calc-sum> = <calc-product> [ [ '+' | '-' ] <calc-product> ]* <calc-product> = <calc-value> [ '*' <calc-value> | '/' <number> ]* <calc-value> = <number> | <dimension> | <percentage> | ( <calc-sum> )
Where a <dimension> is a dimension.
In addition, whitespace is required on both sides of the + and - operators. (The * and / operaters can be used without whitespace around them.)
UAs must support calc() expressions of at least 20 terms, where each NUMBER, DIMENSION, or PERCENTAGE is a term. If a calc() expression contains more than the supported number of terms, it must be treated as if it were invalid.
.
-
I think it's just out of date. Last year's revision of the grammar suggested that you could in fact nest `calc()` function calls: http://www.w3.org/TR/css3-values/#calc-syntax Of course, that was last year. I don't know if it was ever implemented that way. – BoltClock Dec 22 '14 at 05:51
-
@BoltClock: The spec you linked is of 30-Jul-2013. The spec which I linked to is the "dev" of 19-Dec-2014. So, I think the correct reference would be to the spec you linked to. But, even that does not say that `calc` can be nested. Maybe I am not looking hard! – Abhitalks Dec 22 '14 at 05:57
-
You can see the `calc` production nested within another production... that is nested within the `calc` production. Anyway, it turns out that implementations do follow what's in the ED so I think they just changed the spec to match. – BoltClock Dec 22 '14 at 06:05
-
@BoltClock: Hmmm.. yes that is confusing because it says *Components of a ‘calc()’ expression can be literal values, ‘attr()’ or ‘calc()’ expressions, or
values that resolve to one of the preceding types* but later in the document it uses *calc* as nested production: *unit : [ NUMBER | DIMENSION | PERCENTAGE | "(" S* sum S* ")" | calc | attr ]*.. Well, I think I should update the answer. Thank you indeed for pointing it out. – Abhitalks Dec 22 '14 at 06:08 -
2Yup, it was just an oversight in the grammar. Source: I'm the spec editor, and I recently fixed this. – Xanthir Apr 04 '16 at 23:49
-
4Looks like Internet Explorer 11 doesn't support nested calcs e.g. calc(((100% - calc((80 / 100) * (100% + 17px))) / 2) + 17px / 2).. Chrome doesn't have a problem. – Kieran Ryan Jun 23 '17 at 09:55
-
Thanks, the comment about required spaces around + and - operators solved my issues ;) – Daniel Abril Jun 28 '21 at 08:54