Course contentsShow
← Web Development
Lesson 473 of 2,9828. Advanced CSS Selectors and FeaturesFree lesson

Custom Properties in Calculations

Using custom properties inside calc(), min(), max(), and clamp() functions for dynamic values.

Custom Properties in Calculations

You'll learn how to use CSS custom properties as dynamic inputs inside mathematical functions like calc(), min(), max(), and clamp().

The Core Idea

Custom properties don't just store static values—they can be plugged directly into CSS calculation functions. This creates a powerful system where you change one variable and multiple calculated values update automatically across your design.

Think of it like a spreadsheet: you define a cell with a base number, then use formulas that reference that cell. Change the base number once, and all the formulas recalculate instantly.

How It Works

You can use var() inside any calculation function:

:root {
  --base-spacing: 1rem;
  --multiplier: 2;
}

.card {
  /* Using custom properties in calc() */
  padding: calc(var(--base-spacing) * var(--multiplier));
  
  /* Inside min() and max() */
  width: min(var(--base-spacing) * 30, 100%);
  
  /* Inside clamp() for fluid sizing */
  font-size: clamp(
    calc(var(--base-spacing) * 0.875),
    2vw,
    calc(var(--base-spacing) * 1.25)
  );
}

This pattern shines when building design systems. You can define base values at :root and let components calculate their own spacing, sizing, and typography based on those shared values. Change --base-spacing once, and every calculation using it updates proportionally.

You can also scope calculations to specific contexts, overriding custom properties inside containers to create localized variations without rewriting all your math.

Key Takeaway: Custom properties become truly powerful when combined with calculation functions—they let you build flexible, mathematical design systems where changing a single variable cascades through all dependent calculations.