CSS Calc Deep Dive
In this page:
selector {
width: calc(value1 operator value2); /* + - * / */
}
Mixing Unit Types in One Expression
calc() shines when combining unit types that cannot normally interact, such as adding a fixed pixel value to a percentage-based width, letting you express layout rules that would otherwise require JavaScript.
Example: Mixing Unit Types in One Expression
<style>
.box {
width: calc(100% - 40px);
background: lightblue;
padding: 10px;
}
</style>
<div class="box">Percentage and pixels combined in one expression</div>
Nesting calc() Expressions
calc() expressions can reference other calc() expressions or contain grouped sub-expressions in parentheses, which is useful when a value depends on multiple derived intermediate calculations.
Example: Nesting calc() Expressions
<style>
.box {
width: calc(calc(100% - 20px) / 2);
background: lightblue;
padding: 10px;
}
</style>
<div class="box">A calc() expression built from another calc()</div>
Responsive Calculations
calc() paired with viewport units and min/max width constraints allows building fluid layouts that respond smoothly across breakpoints instead of jumping abruptly at fixed media query thresholds.
Example: Responsive Calculations
<style>
h1 {
font-size: calc(1rem + 2vw);
}
</style>
<h1>Scales smoothly across breakpoints</h1>
calc() With Custom Properties
Custom properties become far more powerful once combined with calc(), letting a single base value drive multiple derived measurements throughout a stylesheet from one central source of truth.
Example: calc() With Custom Properties
<style>
.wrapper {
border: 2px dashed #999;
padding: 10px;
}
:root {
--cc-base-unit: 8px;
}
.box {
margin: calc(var(--cc-base-unit) * 3);
background: lightblue;
padding: 10px;
}
</style>
<div class="wrapper">
<div class="box">Derived from one central custom property</div>
</div>
Practical Layout Patterns With calc()
Common real-world uses of calc() include sticky footers, full-height sections that account for a fixed header, and equal-width columns that subtract shared gutters, patterns that appear repeatedly across production websites.
Example: Practical Layout Patterns With calc()
<style>
:root {
--header-height: 60px;
}
.content {
min-height: calc(100vh - var(--header-height));
background: lightblue;
}
</style>
<div class="content">Fills the screen minus the fixed header</div>
- Nesting calc() unnecessarily, calc() expressions can contain plain arithmetic sub-expressions without wrapping them in another calc().
- Trying to mix a percentage with a viewport unit and expecting a single simple conversion, the browser resolves each unit against its own reference before combining them, which can produce unexpected results if the two references differ.
- Overwriting a custom property used inside calc() somewhere unrelated in the stylesheet, causing every calc() expression referencing it to silently change.
- calc() can freely mix percentages, pixels, viewport units, and custom properties in a single expression.
- calc() expressions can be nested inside each other, though flattening them into one expression is usually clearer.
- Storing shared values in custom properties and referencing them inside calc() creates a maintainable, centrally controlled sizing system.
calc() is supported by all modern browsers, and nested calc() expressions and custom property references inside calc() are supported everywhere calc() itself is supported.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: