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);
}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
Aspect Ratio CalculatorSimplify a width-to-height ratio, show its decimal value, and derive a matching height for a new width.Border Radius PreviewPreview four-corner border-radius values on a live box and copy the generated shorthand border-radius CSS declaration.Box Shadow GeneratorBuild CSS box-shadow values with offset, blur, spread, color, opacity, and inset controls, with a live preview of the result.CSS Color Name LookupTranslate between CSS keyword color names and hex values with RGB and HSL conversions, exact name matching, and nearest-color suggestions.
Report a problem with this exampleOpens a pre-filled issue on GitHub.