If you’ve been writing CSS for a while, you’ve probably come across the calc() function. Most developers know it exists, but many only use it occasionally or avoid it altogether because they think it’s complicated.
The truth is, calc() is one of the most useful CSS functions for creating flexible, responsive layouts. It lets the browser perform mathematical calculations, allowing you to combine different units like percentages, pixels, viewport units, and CSS variables without writing JavaScript.
In this tutorial, you’ll learn how to use CSS calc() through ten practical examples that you can immediately apply in your own projects.
What is CSS calc()?
The calc() function performs mathematical calculations directly in CSS.
It supports four basic operators:
- Addition (
+) - Subtraction (
-) - Multiplication (
*) - Division (
/)
For example:
.container{
width: calc(100% - 40px);
}
Instead of manually calculating widths, the browser subtracts 40 pixels from the available width.
The real power of calc() comes from mixing different units together.
For example:
- Percentage + Pixels
- Viewport units + Pixels
- CSS Variables + Pixels
- REM + Viewport Width
Without calc(), many of these layouts would require additional wrappers or JavaScript.
Why Use CSS calc()?
Using calc() helps you:
- Build responsive layouts
- Avoid hardcoded dimensions
- Mix different CSS units
- Reduce unnecessary JavaScript
- Make your stylesheets easier to maintain
Let’s look at some real-world examples.
Example 1: Creating Responsive Widths
Suppose you want a container to fill the available width while leaving 20 pixels of space on both sides.
.container{
width: calc(100% - 40px);
margin: auto;
}
Instead of calculating percentages manually, CSS does the math for you.
This is one of the simplest and most common uses of calc().
Example 2: Fixed Sidebar with Flexible Content
Dashboard layouts often have a fixed-width sidebar.
.sidebar{
width:280px;
}
.content{
width:calc(100% - 280px);
}
As the browser window grows or shrinks, the content area automatically adjusts while the sidebar remains fixed.
No JavaScript required.
Example 3: Full Height Minus Header
Imagine your website has a fixed navigation bar that is 80 pixels tall.
header{
height:80px;
}
main{
height:calc(100vh - 80px);
}
The content always fills the remaining height of the screen.
This technique works perfectly for:
- Admin dashboards
- Landing pages
- Mobile apps
- Documentation websites
Example 4: Creating Equal Grid Columns
Suppose you’re building a four-column card layout with a total gap of 60 pixels.
.card{
width:calc((100% - 60px) / 4);
}
The browser subtracts the total gap first and then divides the remaining width equally among four cards.
This creates perfectly balanced layouts without guessing percentages.
Example 5: Responsive Padding
Responsive spacing often requires different padding on different screen sizes.
Instead of media queries, you can use:
section{
padding:calc(20px + 2vw);
}
The padding increases gradually as the viewport becomes wider, creating a more natural responsive design.
Example 6: Positioning Elements Precisely
Suppose you have a popup that is exactly 300 pixels wide.
.popup{
width:300px;
left:calc(50% - 150px);
}
The browser automatically positions the popup in the center.
Although Flexbox and Grid are preferred for most layouts today, this technique is still useful when working with absolutely positioned elements.
Example 7: Creating a Sticky Footer
Keeping the footer at the bottom of short pages is a common challenge.
main{
min-height:calc(100vh - 120px);
}
If the combined height of your header and footer is 120 pixels, the content automatically fills the remaining space.
The footer always stays where it belongs.
Example 8: Combining CSS Variables with calc()
One of the biggest advantages of calc() is how well it works with CSS custom properties.
:root{
--header-height:80px;
}
main{
height:calc(100vh - var(--header-height));
}
If your header height changes later, you only update the variable.
Every calculation updates automatically.
This keeps your CSS much easier to maintain.
Example 9: Mixing Multiple Units
One of the strengths of calc() is combining different measurement units.
.hero{
padding-top:calc(4rem + 5vh);
}
Here, the browser combines a fixed rem value with a viewport-based value.
This creates layouts that scale naturally across different screen sizes.
Example 10: Building a Responsive Dashboard Layout
Let’s put everything together.
Imagine you’re creating an admin dashboard with:
- A fixed header
- A fixed sidebar
- A content area that fills the remaining space
:root{
--header:70px;
--sidebar:260px;
}
.wrapper{
display:flex;
height:calc(100vh - var(--header));
}
.sidebar{
width:var(--sidebar);
}
.content{
width:calc(100% - var(--sidebar));
}
Without calc(), you’d likely need additional wrappers or JavaScript to achieve the same layout.
With just a few lines of CSS, the entire interface remains responsive and easy to maintain.
Common Mistakes
Forgetting Spaces Around Operators
This won’t work:
width:calc(100%-20px);
Correct syntax:
width:calc(100% - 20px);
Always include spaces around mathematical operators.
Mixing Incompatible Values
This is invalid CSS:
width:calc(red + 20px);
calc() only works with numeric values and compatible CSS units.
Overcomplicating Your Calculations
While calc() is powerful, readability should always come first.
Instead of writing long mathematical expressions, combine it with CSS variables.
:root{
--sidebar:280px;
}
.content{
width:calc(100% - var(--sidebar));
}
Future you—and anyone else working on the project—will appreciate the cleaner code.
Browser Support
The CSS calc() function has excellent support across all modern browsers, including Chrome, Firefox, Edge, Safari, and mobile browsers.
Unless you’re targeting very old browsers, it’s perfectly safe to use in production websites.
Does calc() Affect Performance?
Not in any noticeable way.
Browsers evaluate these calculations as part of their normal layout process, and the overhead is extremely small.
In real-world websites, using calc() has virtually no measurable performance impact.
Choose it based on readability and maintainability, not performance concerns.
Final Thoughts
The CSS calc() function is one of those features that quietly solves everyday layout problems. From responsive widths and fixed sidebars to full-height sections and reusable CSS variables, it helps you build cleaner, more flexible stylesheets with less code.
The next time you find yourself manually calculating widths, adding unnecessary wrapper elements, or reaching for JavaScript to solve a layout issue, consider whether calc() can handle it instead.
In many cases, the answer is yes.
Do you have a favorite way to use calc()? Share it in the comments below—I’d love to hear how you’re using it in your own projects.




