What is calc()?
The calc() CSS function lets you perform mathematical calculations to determine CSS property values. It can mix different units (%, px, em, vw, etc.) and works anywhere a numeric value is expected. This enables truly responsive designs that adapt dynamically without JavaScript.
Basic Syntax
Supported Operators
| Operator | Operation | Example | Notes |
|---|---|---|---|
| + | Addition | calc(10px + 20px) | Spaces required around operator |
| - | Subtraction | calc(100% - 50px) | Spaces required around operator |
| * | Multiplication | calc(2rem * 1.5) | At least one number unitless |
| / | Division | calc(100% / 3) | Right side must be unitless |
Mixing Different Units
Common Use Cases
Advanced Techniques
Responsive Typography with calc()
Grid & Flexbox Layouts
Spacing Systems
Performance & Best Practices
Browser Support & Fallbacks
Common Mistakes & Gotchas
- Missing spaces around + and - –
calc(100%-20px)is invalid (needs spaces) - Multiplication with units on both sides –
calc(10px * 20px)is invalid - Division with units on right side –
calc(100% / 2px)is invalid - Not providing fallbacks – Older browsers ignore calc() entirely
- Over-nesting – Unnecessary calc() inside calc()
- Using calc() for static values – Just calculate manually
- Forgetting parentheses – Order of operations matters
- Using calc() in media queries – Doesn't work in media query conditions
Real-World Examples
Advanced: calc() with Trigonometric Functions
Conclusion
calc() is an essential CSS function for modern, responsive web design. It enables dynamic layouts that adapt to different screen sizes, mixed units, and complex calculations without JavaScript. Combine with CSS variables, clamp(), min(), and max() for even more powerful responsive designs. Remember to provide fallbacks for older browsers and keep calculations readable for maintainability.