calc in CSS

calc in CSS is a function that performs calculations directly in CSS values. It can add, subtract, multiply, and divide values, and it is especially useful because it can combine different units such as percentages, pixels, rem, vw, and custom properties.

Without calc, many layouts need hardcoded values or extra wrappers. With calc, CSS can express relationships such as full width minus spacing, viewport height minus header height, or a flexible column minus a fixed sidebar.

Basic calc Syntax

The syntax uses calc() with a mathematical expression inside. Addition and subtraction need spaces around the operator.

.box {
  width: calc(100% - 2rem);
}

This box is the full parent width minus 2rem. The spaces around the minus sign are required. Without spaces, the expression may be invalid.

Supported Operators

calc supports addition, subtraction, multiplication, and division. Addition and subtraction are the most common in layout code.

OperatorExampleMeaning
+calc(1rem + 10px)add values
calc(100% – 2rem)subtract values
*calc(2rem * 1.5)multiply value
/calc(100% / 3)divide value

Even though multiplication and division are available in modern CSS calculations, addition and subtraction remain the everyday workhorses for layout.

Mixing Units

The biggest advantage of calc is mixing units that CSS could not simplify ahead of time. For example, percentages depend on the parent size, while rem depends on font size.

.content {
  width: calc(100% - 280px);
}

This can represent a content area that takes the remaining space after a fixed sidebar. The browser calculates the final value after it knows the parent width.

Spacing with calc

calc can create containers that keep consistent side spacing while staying fluid.

.container {
  width: min(1120px, calc(100% - 2rem));
  margin-inline: auto;
}

The container never exceeds 1120px and stays 2rem narrower than the viewport on small screens. This is a clean modern container pattern.

Viewport Height Layouts

calc is useful when a section must account for a fixed header or footer.

.main {
  min-height: calc(100dvh - 72px);
}

The main area takes the dynamic viewport height minus a 72px header. This is useful for dashboards, landing screens, and app-like layouts.

Positioning with calc

calc can adjust positioned elements without hardcoding several separate values.

.badge {
  position: absolute;
  top: calc(100% - 12px);
  left: calc(50% - 12px);
}

This places a badge relative to its parent dimensions while accounting for the badge size. It is more expressive than guessing magic numbers.

calc with Custom Properties

Custom properties make calc more maintainable because the repeated values can be named.

:root {
  --sidebar-width: 280px;
  --page-gap: 2rem;
}

.content {
  width: calc(100% - var(--sidebar-width) - var(--page-gap));
}

If the sidebar width changes, the calculation updates everywhere that uses the variable. This is useful in design systems and larger CSS files.

calc with clamp

calc is often used inside clamp to create smoother fluid values.

h1 {
  font-size: clamp(2rem, calc(1.5rem + 4vw), 5rem);
}

The preferred value starts with a rem base and adds viewport-based growth. The minimum and maximum keep the result safe.

Grid and Flexbox Calculations

Modern layout tools reduce the need for calc, but it is still useful when a track or item must account for a known gap or fixed region.

.card {
  flex-basis: calc((100% - 2rem) / 3);
}

This creates three flex items per row while accounting for a total 2rem gap. CSS Grid can often do this more cleanly, but calc is still useful in existing flex layouts.

Spacing Rules Matter

The most common calc syntax mistake is forgetting spaces around plus and minus. Multiplication and division are less sensitive in the same way, but consistent spacing keeps expressions readable.

.valid {
  width: calc(100% - 2rem);
}

.invalid {
  width: calc(100%-2rem);
}

The second example is invalid because the minus operator is not separated by spaces. This tiny detail causes many beginner bugs.

When Not to Use calc

Do not use calc just to make CSS look clever. If flexbox, grid, gap, max-width, min(), max(), or clamp() describe the layout more clearly, use those tools instead.

For example, a responsive card grid is usually easier with repeat(auto-fit, minmax()) than with several calc-based width formulas.

calc in CSS Variables

Custom properties can store full values or parts of a calculation. This makes layout formulas easier to adjust because the meaningful numbers have names.

:root {
  --header-height: 72px;
  --content-padding: 2rem;
}

.app-main {
  min-height: calc(100dvh - var(--header-height));
  padding: var(--content-padding);
}

The calculation is readable because the fixed header height has a name. If the header changes, the layout can be updated without hunting for repeated numbers.

Unitless Variables in calc

Sometimes a custom property stores a unitless number. In that case, calc can multiply it by a unit to create a final length.

:root {
  --columns: 3;
  --gap: 1rem;
}

.item {
  width: calc((100% - (var(--columns) - 1) * var(--gap)) / var(--columns));
}

This kind of formula can be useful, but it can also become difficult to read. If the layout can be expressed with CSS Grid, grid is usually cleaner.

calc in Transforms

calc can be used in transform values too. This is useful when an element needs to move by a percentage plus a fixed correction.

.tooltip {
  transform: translateX(calc(-50% + 8px));
}

The tooltip moves left by half its width, then shifts right by 8px. This is more precise than adding another wrapper just for positioning correction.

calc with Environment Variables

On devices with notches or safe areas, CSS environment variables can be used in calculations. This helps fixed UI avoid unsafe screen areas.

.mobile-footer {
  padding-bottom: calc(1rem + env(safe-area-inset-bottom));
}

The footer gets normal padding plus any extra safe-area inset required by the device. This is useful for mobile web apps and fixed bottom bars.

Practical calc Checklist

  • Use spaces around plus and minus.
  • Name repeated values with custom properties.
  • Avoid formulas that are harder than the layout problem.
  • Prefer grid or flexbox when they solve the layout directly.
  • Inspect computed values when debugging.
  • Avoid unexplained magic numbers.

Real Example: Sidebar and Main Content

A common layout has a fixed sidebar and a flexible main area. CSS Grid can often handle this directly, but calc is still useful in older layouts or special cases.

.sidebar {
  width: 280px;
}

.main {
  width: calc(100% - 280px);
}

This says the main area should take the remaining width. In new layouts, grid-template-columns: 280px 1fr is usually cleaner, but this example shows the relationship calc is expressing.

Real Example: Full Width Breakout

Sometimes content inside a centered wrapper needs to visually break out to the full viewport width. calc can help offset the wrapper constraints.

.full-bleed {
  width: 100vw;
  margin-left: calc(50% - 50vw);
}

This pattern lets an image or banner stretch edge to edge while still living inside article markup. It should be tested carefully because scrollbars and overflow behavior can affect viewport width.

Real Example: Form Widths

Forms sometimes need an input and a fixed-width button in the same row. calc can size the input based on the button width and gap.

.email-input {
  width: calc(100% - 8rem - 0.75rem);
}

.email-button {
  width: 8rem;
}

Flexbox can often solve this more elegantly with flex: 1, but calc can still be useful when working inside existing CSS constraints.

Nested calc

Modern CSS can simplify nested calculations, but deeply nested formulas are hard to maintain. Prefer clear variables and simple expressions.

.box {
  width: calc(100% - var(--sidebar) - (2 * var(--gap)));
}

This is readable because the variables explain the formula. Without meaningful names, the same expression would look like a random collection of numbers.

calc and Box Sizing

When using calc for widths, remember that padding and borders can affect the final rendered size. With box-sizing: border-box, width includes padding and border. Without it, padding and border are added outside the declared width.

* {
  box-sizing: border-box;
}

.panel {
  width: calc(100% - 2rem);
  padding: 1rem;
}

This makes the panel easier to reason about because the calculated width includes its padding. Many modern resets set border-box globally for this reason.

calc in CSS Grid Gaps

calc can account for gaps when layout items need exact percentages. This appears in older grid systems and custom flex layouts.

.column {
  width: calc((100% - 3rem) / 4);
}

This creates four equal columns after reserving 3rem for gaps. In modern CSS, native grid gap is often cleaner, but understanding the calculation helps when maintaining older layouts.

calc Readability Rule

If a calc expression needs more than a few terms, consider simplifying the layout or naming parts with custom properties. A readable formula is useful. A clever formula that nobody wants to edit is technical debt.

Keep the relationship obvious.

Keep it clear.

Debugging calc

Browser developer tools show the computed value after calc is resolved. If the result looks wrong, inspect the parent size, custom property values, and whether the property accepts the calculated type.

  • Check spaces around plus and minus operators.
  • Check whether custom properties contain valid units.
  • Inspect the computed value in developer tools.
  • Confirm the property accepts the final calculated value.
  • Prefer grid or flexbox when they express the layout more directly.

Common calc Mistakes

  • Writing calc(100%-20px) without spaces.
  • Using calc when normal grid or flex behavior is cleaner.
  • Forgetting units in custom property values.
  • Creating hard-to-read formulas with too many nested calculations.
  • Expecting calc to know content size in every situation.
  • Using magic numbers without explaining the relationship.

calc in CSS FAQ

What does calc do in CSS?

calc lets CSS perform mathematical calculations inside property values.

Can calc mix percentages and pixels?

Yes. That is one of its most useful features because the browser resolves the final value at layout time.

Why is my calc not working?

Check spaces around plus and minus, valid units, custom property values, and whether the property accepts that calculated value.

Can calc use CSS variables?

Yes. calc works well with custom properties through var().


Continue learning CSS in order
Follow the topic sequence with the previous and next lesson.