The calc()
CSS function lets you perform calculations when specifying CSS property values. It can be used with <length>
, <frequency>
, <angle>
, <time>
, <percentage>
, <number>
, or <integer>
values.
The calc()
CSS function lets you perform calculations when specifying CSS property values. It can be used with <length>
, <frequency>
, <angle>
, <time>
, <percentage>
, <number>
, or <integer>
values.
/* property: calc(expression) */ width: calc(100% - 80px);
The calc()
function takes a single expression as its parameter, with the expression's result used as the value. The expression can be any simple expression combining the following operators, using standard operator precedence rules:
+
Addition.
-
Subtraction.
*
Multiplication. At least one of the arguments must be a <number>
.
/
Division. The right-hand side must be a <number>
.
The operands in the expression may be any <length>
syntax value. You can use different units for each value in your expression, if you wish. You may also use parentheses to establish computation order when needed.
+
and -
operators must be surrounded by whitespace. For instance, calc(50% -8px)
will be parsed as "a percentage followed by a negative length" — which is an invalid expression — while calc(50% - 8px)
is "a percentage followed by a subtraction operator and a length". Likewise, calc(8px + -50%)
is treated as "a length followed by an addition operator and a negative percentage".*
and /
operators do not require whitespace, but adding it for consistency is recommended.auto
had been specified.calc()
functions, in which case the inner ones are treated as simple parentheses.0
to mean 0px
(or another length unit); instead, you must use the version with the unit: margin-top: calc(0px + 20px);
is valid, while margin-top: calc(0 + 20px);
is invalid.calc()
function cannot directly substitute the numeric value for percentage types; for instance calc(100 / 4)%
is invalid, while calc(100% / 4)
is valid.When calc()
is used for controlling text size, be sure that one of the values includes a relative length unit, for example:
h1 { font-size: calc(1.5rem + 3vw); }
This ensures that text size will scale if the page is zoomed.
When calc()
is used where an <integer>
is expected, the value will be rounded to the nearest integer. For example:
.modal { z-index: calc(3 / 2); }
This will give .modal
a final z-index
value of 2.
calc()
makes it easy to position an object with a set margin. In this example, the CSS creates a banner that stretches across the window, with a 40-pixel gap between both sides of the banner and the edges of the window:
.banner { position: absolute; left: 40px; width: calc(100% - 80px); border: solid black 1px; box-shadow: 1px 2px; background-color: yellow; padding: 6px; text-align: center; box-sizing: border-box; }
<div class="banner">This is a banner!</div>
Another use case for calc()
is to help ensure that form fields fit in the available space, without extruding past the edge of their container, while maintaining an appropriate margin.
Let's look at some CSS:
input { padding: 2px; display: block; width: calc(100% - 1em); } #formbox { width: calc(100% / 6); border: 1px solid black; padding: 4px; }
Here, the form itself is established to use 1/6 of the available window width. Then, to ensure that input fields retain an appropriate size, we use calc()
again to establish that they should be the width of their container minus 1em. Then, the following HTML makes use of this CSS:
<form> <div id="formbox"> <label for="misc">Type something:</label> <input type="text" id="misc" name="misc" /> </div> </form>
calc()
with CSS VariablesYou can also use calc()
with CSS variables. Consider the following code:
.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.
Specification |
---|
CSS Values and Units Module Level 4 # calc-func |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
calc |
2619 | 12 |
16["Before Firefox 59calc() is not supported in rgb() and other color functions.", "Before Firefox 57 calc(1*2*3) is not parsed successfully.", "Firefox 57 increased the number of places calc() could substitute another value. See bug 1350857."] |
9 | 15 | 76 | ≤37≤37 | 28 |
16["Before Firefox 59calc() is not supported in rgb() and other color functions.", "Before Firefox 57 calc(1*2*3) is not parsed successfully.", "Firefox 57 increased the number of places calc() could substitute another value. See bug 1350857."] |
14 | 76 | 1.5 |
gradient_color_stops |
19 | 12 | 19 | 9 | 15 | 6 | 37 | 28 | 19 | 15 | 6 | 1.5 |
nested |
51 | 16 | 48 | No | 38 | 11 | 51 | 51 | 48 | 41 | 11 | 5.0 |
number_values |
31 | 12 | 48 | 9 | 18 | 6 | 4.4.3 | 31 | 48 | 18 | 6 | 2.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/calc