CSS Advanced Features

CSS calc() Function

Master CSS calculations with the calc() function. Create dynamic, responsive layouts by combining different units and mathematical operations.

What does calc() do in CSS?

calc() performs maths inside CSS values and can mix units, for example width: calc(100% - 250px) for a column that fills whatever space a 250px sidebar leaves. It supports +, -, * and /, and works anywhere a length, number, angle or time is allowed.

.main    { width: calc(100% - 250px); }
.gap     { margin-top: calc(var(--space) * 2); }
.overlap { top: calc(50% - 1rem); }

Interactive calc() Function Demo

Code Editor

<div class="layout">
  <aside>250px sidebar</aside>
  <main>calc(100% - 250px)</main>
</div>
<div class="centered">top: calc(50% - 20px)</div>

Preview

Basic calc() Operations

Mathematical Operations

/* Addition */ .element {
  width: calc(100px + 50px); /* = 150px */
  height: calc(100% + 2rem);
  margin: calc(1rem + 10px);
}

/* Subtraction */ .container {
  width: calc(100% - 2rem);
  height: calc(100vh - 60px);
  padding: calc(2rem - 5px);
}

/* Multiplication */ .grid-item {
  width: calc(100% / 3);
  font-size: calc(1rem * 1.5);
  line-height: calc(1.5 * 1.2);
}

/* Division */ .card {
  width: calc(100% / 4);
  height: calc(300px / 2);
  margin: calc(2rem / 3);
}

Basic arithmetic operations: +, -, *, / with proper spacing around operators.

Mixed Units

/* Combine different units */
.responsive-width {
  width: calc(50% - 2rem);
}

.fluid-height {
  height: calc(100vh - 100px);
}

.flexible-margin {
  margin: calc(5vw + 1rem);
}

.dynamic-font {
  font-size: calc(1rem + 2vw);
}

.complex-spacing {
  padding: calc(1em + 5px + 1vw);
}

Combine percentages, pixels, rems, viewport units, and more

Operator Precedence

/* Use parentheses for grouping */
.precedence-example {
  /* Without parentheses */
  width: calc(100% / 3 + 2rem);
  /* = (100% / 3) + 2rem */

  /* With parentheses */
  height: calc((100% + 2rem) / 3);
  /* = (100% + 2rem) / 3 */

  /* Complex expression */
  margin: calc((
    100% - 4rem
  ) / 3 + 1rem);
}

Use parentheses to control calculation order

Layout Applications

Responsive Layouts

/* Two-column layout with gap */
.sidebar {
  width: 250px;
  flex-shrink: 0;
}

.main-content {
  width: calc(100% - 250px - 2rem);
  /* Total width - sidebar width - gap */
}

/* Three-column grid with gaps */
.grid-item {
  width: calc((100% - 4rem) / 3);
  /* (Total - gaps) / columns */
}

/* Full height minus header and footer */
.main-area {
  height: calc(100vh - 60px - 40px);
  /* Viewport height - header - footer */
}

/* Container with padding */
.content-container {
  width: calc(100% - 4rem);
  max-width: calc(1200px - 4rem);
  margin: 0 auto;
}

Create flexible layouts that adapt to content and screen sizes.

Grid Systems

/* Dynamic grid columns */
.grid {
  display: grid;
  grid-template-columns:
    repeat(auto-fit,
      minmax(calc(300px - 2rem), 1fr));
  gap: 2rem;
}

/* Responsive column spans */
.grid-item-large {
  grid-column: span
    calc(var(--cols) / 2);
}

/* Flexible row heights */
.grid-row {
  grid-template-rows:
    calc(50vh - 2rem) 1fr auto;
}

Build dynamic grid systems with calculated dimensions

Typography Scaling

/* Fluid typography */
.fluid-text {
  font-size: calc(1rem + 1vw);
  line-height: calc(1.5 + 0.3vw);
}

/* Responsive headings */
h1 {
  font-size: calc(2rem + 3vw);
  margin: calc(1rem + 1vw) 0;
}

/* Adaptive spacing */
.text-block {
  padding: calc(1rem + 2vw);
  margin-bottom: calc(0.5rem + 1vh);
}

/* Responsive borders */
.border-element {
  border-width: calc(1px + 0.1vw);
}

Create truly responsive typography that scales smoothly

Advanced Techniques

CSS Variables with calc()

/* Define custom properties */
:root {
  --base-size: 1rem;
  --scale-factor: 1.5;
  --container-width: 1200px;
  --sidebar-width: 250px;
  --gap: 2rem;
}

/* Use variables in calculations */
.component {
  font-size: calc(var(--base-size) * var(--scale-factor));
  width: calc(var(--container-width) - var(--sidebar-width) - var(--gap));
  padding: calc(var(--base-size) * 2);
}

/* Dynamic theme switching */
.theme-dark {
  --base-size: 1.1rem;
  --scale-factor: 1.6;
}

/* Responsive variables */
@media (max-width: 768px) {
  :root {
    --sidebar-width: 0px;
    --gap: 1rem;
  }
}

Combine CSS variables with calc() for dynamic, themeable designs.

Complex Expressions

/* Multi-step calculations */
.complex-layout {
  width: calc((
    100% - 4rem
  ) / 3 - 1rem);
}

/* Nested calc() functions */
.nested-calc {
  font-size: calc(
    calc(1rem * 1.5) + 0.5vw
  );
}

/* Mathematical constants */
.circle {
  width: calc(100px * 3.14159);
  border-radius: 50%;
}

/* Conditional logic simulation */
.responsive-padding {
  padding: calc(
    1rem + (2vw - 1rem) * (
      (100vw - 320px) / (1200 - 320)
    )
  );
}

Build complex mathematical expressions for advanced layouts

Performance Considerations

/* ✅ Efficient - simple calculations */
.good {
  width: calc(100% - 2rem);
  height: calc(50vh + 100px);
}

/* ⚠️ Less efficient - complex nesting */
.complex {
  width: calc(
    calc(
      calc(100% / 3) + 1rem
    ) * 2 - 5px
  );
}

/* ✅ Better - use variables */
:root {
  --column-width: calc(100% / 3);
}

.optimized {
  width: calc(var(--column-width) * 2 - 5px);
}

Keep calculations simple for better performance

Common Use Cases & Best Practices

Best Practices

  • • Always include spaces around operators
  • • Use parentheses for complex expressions
  • • Combine with CSS variables for flexibility
  • • Test across different screen sizes
  • • Consider fallbacks for older browsers
  • • Keep calculations readable and simple

Common Applications

  • • Responsive layout calculations
  • • Fluid typography scaling
  • • Grid system implementations
  • • Full-height layouts
  • • Dynamic spacing systems
  • • Component size variations

CSS calc() — step by step

1 / 4
1

Basic calc() Syntax

calc() evaluates math expressions: width: calc(100% - 240px) subtracts a fixed sidebar from the full width. Spaces around operators are required.

1 of 4
💡clamp() for Fluid Typography
font-size: clamp(1rem, 2.5vw, 1.5rem) sets a minimum of 1rem, scales fluidly with viewport, and caps at 1.5rem — eliminating the need for breakpoint-based font size changes.

🎯 Quick Check

Which function constrains a value between a minimum and maximum?

CSS calc(): summary

Key points

  • + and - must have spaces on both sides: calc(100% - 10px), not calc(100%-10px).
  • When multiplying or dividing, one side must be a plain number.
  • calc() works well with CSS variables: calc(var(--size) * 1.5).

Frequently asked questions

›Why is my calc() not working?

The most common cause is missing spaces around + or -. Also check that you're not dividing by a value with a unit.

›Can I use calc() with CSS variables?

Yes. calc(var(--header-height) + 1rem) is a very common pattern.