calc() for Dynamic Calculations
Mix units and perform arithmetic in CSS using the calc() function for dynamic values.
calc() for Dynamic Calculations is a free CSS Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is calc()?
calc() allows you to perform arithmetic directly in CSS property values. You can mix different units (px, %, rem, vw) in a single calculation.
Basic calc() Syntax
Supports addition (+), subtraction (-), multiplication (*), and division (/). Spaces around + and - operators are required.
.sidebar {
width: calc(100% - 240px);
/* 100% of parent minus 240px sidebar */
}Mixing Units
The superpower of calc() is combining incompatible units that would otherwise be impossible to mix:
.hero {
height: calc(100vh - 64px);
/* full viewport minus nav height */
}
.column {
width: calc(33.333% - 24px);
/* one third minus gap */
}calc() with CSS Variables
Combine calc() with CSS custom properties for dynamic design tokens:
:root { --nav-height: 64px; }
.main {
min-height: calc(100dvh - var(--nav-height));
padding-top: var(--nav-height);
}Nested calc()
calc() can be nested, though it is rarely needed since each context already evaluates math:
.element {
width: calc(calc(100% / 3) - 16px);
/* equivalent to: calc(100% / 3 - 16px) */
}calc() for Fluid Typography
Use calc() to create a linear interpolation between two sizes:
h1 {
/* 24px at 320px viewport, 48px at 1280px viewport */
font-size: calc(24px + (48 - 24) * ((100vw - 320px) / (1280 - 320)));
}
/* Note: clamp() is cleaner for this use case */Division in calc()
Division requires the divisor to be a number without units:
.thirds {
width: calc(100% / 3); /* valid */
/* width: calc(100% / 3%); — INVALID */
}calc() with Grid and Flexbox
Useful inside track definitions and flex-basis:
.grid {
grid-template-columns: calc(50% - 12px) calc(50% - 12px);
/* two equal columns with 24px gap without using fr */
}calc() vs Other Functions
When to use which:
calc(): arbitrary arithmetic, especially mixing unitsmin()/max(): pick smaller/larger of optionsclamp(): bound a fluid value between min and max
Browser Support
calc() is supported in all modern browsers and IE11. It is one of the most widely supported CSS functions. Safe to use without fallbacks in any modern project.
Common calc() Patterns
Practical patterns you will use repeatedly:
/* Full height minus fixed header */
min-height: calc(100dvh - var(--header-h));
/* Half container minus half gap */
width: calc(50% - calc(var(--gap) / 2));
/* Equal thirds with gaps */
flex: 0 0 calc((100% - var(--gap) * 2) / 3);Quick Check
Which calc() expression produces the correct result for "30% of parent minus 20px"?
Recap
calc() enables arithmetic in CSS with mixed units. It powers dynamic layouts like "full viewport minus fixed header" and fluid typography interpolation. Spaces are required around + and - operators. Combine with CSS variables for powerful design token math. Use alongside min/max/clamp for complete fluid sizing control.
Frequently asked questions
Is the “calc() for Dynamic Calculations” lesson free?
Yes — the full text of “calc() for Dynamic Calculations” is free to read here on the web, and the CSS Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO.
What will I learn in “calc() for Dynamic Calculations”?
Mix units and perform arithmetic in CSS using the calc() function for dynamic values. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CSS Academy?
No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “calc() for Dynamic Calculations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CSS Academy lesson?
Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- calc() for Dynamic Calculations
- min() max() and clamp() for Fluid Values
- repeat() and minmax() in Grid
- color-mix() and relative color syntax