CSS Calc for Dynamic Sizing

Use CSS calc function for dynamic sizing combining units and custom properties for flexible layout calculations.

CssConfig Management

Code

:root { --sidebar: 240px; --gap: 16px; }

.main {
  width: calc(100% - var(--sidebar) - var(--gap));
  margin-left: calc(var(--sidebar) + var(--gap));
}

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

CSS Calc for Dynamic Sizing

Use CSS calc function for dynamic sizing combining units and custom properties for flexible layout calculations.

Code

:root { --sidebar: 240px; --gap: 16px; }

.main {
  width: calc(100% - var(--sidebar) - var(--gap));
  margin-left: calc(var(--sidebar) + var(--gap));
}

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

Explanation

calc() mixes units and custom properties inside one length, so a sidebar can be 100% - 2rem and still respond to its container. Expected behavior: the browser evaluates the expression at layout time. Constraint: whitespace is required around the + and - operators, and combining incompatible types (a length plus a color) is invalid.

Interactive Tools for This Topic