CSS / Reference / calc(): Calculate CSS Values and Understand Their Types
CSS calc() performs arithmetic inside a property value. For example, width: calc(100% - 40px) subtracts 40px from the available parent width in this layout. The property receiving the result determines valid types and what a percentage means.
Use compatible units and operators
Put spaces on both sides of binary + and -. Multiplication and division run first unless parentheses change the order. For instance, calc(12px * 2) gives 24px.
width accepts a combination of percentages and lengths. A length cannot be added to or subtracted from a plain number in this context. Other properties can interpret 100% against a different reference.
.card { width: calc(100% - 40px); }
.title { font-size: calc(1rem + 4px); }
Reserve space inside a responsive parent
Save the complete example as calc-example.html in UTF-8 and open it in a browser. The custom property --reserve holds the length to subtract.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Width comparison with calc()</title>
<style>
body {
margin: 1rem;
font: 1rem/1.6 system-ui, sans-serif;
}
.frame {
--reserve: 40px;
width: min(100%, 300px);
background: #e2e8f0;
}
.reference, .target {
box-sizing: border-box;
padding: .5rem;
overflow-wrap: anywhere;
}
.reference { width: 100%; }
.target {
width: calc(100% - var(--reserve));
border: 1px solid #0369a1;
background: #bae6fd;
}
</style>
</head>
<body>
<main>
<h1>Width comparison with calc()</h1>
<div class="frame">
<div class="reference">Full parent width</div>
<div class="target">Width with 40px reserved</div>
</div>
</main>
</body>
</html>
- With a 300px parent, the full-width box is 300px wide and the calculated box is 260px. The calculated width includes padding and the border because the box uses
border-box. - In a narrower viewport, inspect the actual parent width. If it is 280px, the calculated box is 240px. Use the measured parent width rather than the viewport width to predict the result.
- Change
--reservefrom 40px to 20px. The calculated box grows by 20px. Restore the original value after comparing.
Check whether a calculation is accepted
Run these expressions individually in the browser console. The first is accepted for width. The second mixes a length with a unitless number and is invalid here. The third divides two lengths to get 0.5 where the browser supports this form of typed arithmetic.
CSS.supports() tests declaration acceptance. Inspect the element separately for its used width. Infinity and NaN are calculation results that can be handled differently from syntax errors. Avoid division by zero in layout code.
CSS.supports("width", "calc(100% - 40px)");
CSS.supports("width", "calc(100% - 20)");
CSS.supports("opacity", "calc(1px / 2px)");
W3C math function type checking
Diagnose a width that looks wrong
If the outer width is larger than the declared width, check box-sizing. With content-box, padding and borders are added outside the declared content width.
When you only need a maximum width, min(100%, 300px) suffices, as it does for the parent here. Use calc() when you need arithmetic. For spacing between flex or grid children, also consider gap.
See CSS sizing and box-sizing for how borders and padding affect the measured box.




