A card needs three spacing units and a notice needs two. Ordinary CSS can calculate both. The examples share this HTML:

<article class="site-card">
  <h2>Installation guide</h2>
  <p>Start with the first page of your project.</p>
</article>
<aside class="site-notice">
  <h2>Version notice</h2>
  <p>Read the changelog before upgrading.</p>
</aside>
:root {
  --space-unit: 0.5rem;
}
 
.site-card {
  padding: calc(var(--space-unit) * 3);
}
 
.site-notice {
  padding: calc(var(--space-unit) * 2);
}

With a 16px root font size, one unit is 8px and the paddings are 24px and 16px. For these two calculations, the existing code can stay. Give the formula a name when several components need to maintain it together:

:root {
  --space-unit: 0.5rem;
}
 
@function --space(--steps <number>: 1, --unit <length>: 0.5rem)
  returns <length> {
  result: calc(var(--steps) * var(--unit));
}
 
.site-card {
  padding: --space(3, var(--space-unit));
}
 
.site-notice {
  padding: --space(2, var(--space-unit));
}

The function still gives 24px and 16px. What changes is the named calculation, with its multiplier and unit passed at the call site. These examples assume @function support, available since Chrome 139. The 16px setting makes results comparable rather than prescribing a fixed user font size; a cross-browser baseline follows at the end.

Explicit arguments make dependencies visible

Function and parameter names start with --. Call the function directly in a property value: padding: --space(...). Neither @apply nor var(--space(...)) is needed. var(--steps) reads a parameter, result describes the output, and returns <length> constrains its type. Function definition

Here, --space() yields 0.5rem, --space(3) yields 1.5rem, and --space(3, 6px) yields 18px. The <length> type excludes percentages. Types and defaults are optional, but keeping them explains what this interface accepts.

The default 0.5rem belongs to the function definition. Changing the root's --space-unit to 0.75rem will not rewrite it. Pass var(--space-unit) explicitly to use that token. I prefer visible dependencies first, especially in a shared function someone else will maintain.

Why is it still 24px after changing the unit?

Use this snippet independently with the earlier HTML. The card changes its unit to 0.25rem, yet its padding remains 24px:

:root {
  --space-unit: 0.5rem;
  --card-space: calc(var(--space-unit) * 3);
}
 
.site-card {
  --space-unit: 0.25rem;
  padding: var(--card-space);
}

The root resolves var(--space-unit) inside --card-space before that property is inherited. A new unit on the card does not reevaluate the root's reference. Custom-property evaluation and inheritance

Writing calc(var(--space-unit) * 3) on the card itself lets it use its own unit. If the calculation needs shared maintenance, a function can instead read the element's unit when used:

:root {
  --space-unit: 0.5rem;
}
 
@function --local-space(--steps <number>: 1) returns <length> {
  result: calc(var(--space-unit, 0.5rem) * var(--steps));
}
 
.site-card {
  --space-unit: 0.25rem;
  padding: --local-space(3);
}
 
.site-notice {
  padding: --local-space(2);
}

The card now gets 12px, while the notice keeps 16px. With no parameter or local variable named --space-unit, the function reads the custom property on the element using it. Function scope

The earlier --space(3, var(--space-unit)) also produces 12px when the card's unit is 0.25rem. --local-space(3) is shorter but adds an implicit dependency that belongs in its usage contract. A same-named parameter or local variable would shadow the external value. One fewer argument is not necessarily easier to trace.

result does not return early

This version reduces spacing to three quarters when the viewport is at most 40rem wide. That is the example's layout decision, not a universal definition of mobile:

:root {
  --space-unit: 0.5rem;
}
 
@function --responsive-space(--steps <number>: 1) returns <length> {
  --density: 1;
 
  @media (width <= 40rem) {
    --density: 0.75;
  }
 
  result: calc(var(--space-unit, 0.5rem) * var(--steps) * var(--density));
}
 
.site-card {
  padding: --responsive-space(3);
}
 
.site-notice {
  padding: --responsive-space(2);
}

With root and default font sizes both 16px, an 800px viewport gives 24px and 16px. At 600px, the values become 18px and 12px. The local --density does not become a token other CSS can read on the element.

A media condition changes which declarations participate in evaluation. It doesn't make result an early-return instruction: the following function still produces 1rem when its narrow-layout condition matches:

@function --always-one() returns <length> {
  @media (width <= 40rem) {
    result: 0.75rem;
  }
 
  result: 1rem;
}

The later result overrides the earlier one. Unlike JavaScript's return, it does not stop at the first result. Execution model and conditions

I would put a baseline first and conditional overrides later, or use a local variable with one final formula as above. If the components should not share a width condition, keep their media queries separate.

The preceding padding is not a universal fallback

This function requires a multiplier with no default, but receives red:

@function --strict-space(--steps <number>) returns <length> {
  result: calc(var(--steps) * 0.5rem);
}
 
.site-card {
  padding: 24px;
  padding: --strict-space(red);
}

In a supporting browser, red fails the <number> type and the call becomes invalid at computed-value time. padding gets its initial value, 0, rather than selecting the earlier 24px declaration again. Omitting the required argument with --strict-space() also fails. Other properties depend on their own inheritance behavior; zero is not a universal outcome. Invalidity at computed-value time

A default changes the behavior. The earlier --space() gives its multiplier a default of 1, so --space(red) tries that default and yields 0.5rem. The argument-evaluation rules allow this, meaning an erroneous call can appear to work.

Passing the function's type check still leaves the consuming property's constraints. Negative numbers satisfy <number>, but a negative length can serve margin and not padding. Check the call site as well as the type of value the function produces.

What baseline should adoption preserve?

The MDN compatibility data, rechecked October 30, 2026, still records Chrome / Chrome Android and Edge support from 139, no Firefox support, and preview status for Safari / iOS Safari. Preview doesn't establish widespread stable support; the CSSWG editor's draft may also change.

Keep the existing calculation as a cross-browser baseline and isolate the enhancement. This is complete CSS:

:root {
  --space-unit: 0.5rem;
}
 
.site-card {
  padding: calc(var(--space-unit) * 3);
}
 
.site-notice {
  padding: calc(var(--space-unit) * 2);
}
 
@supports at-rule(@function) {
  @function --space(--steps <number>: 1, --unit <length>: 0.5rem)
    returns <length> {
    result: calc(var(--steps) * var(--unit));
  }
 
  .site-card {
    padding: --space(3, var(--space-unit));
  }
 
  .site-notice {
    padding: --space(2, var(--space-unit));
  }
}

The at-rule(@function) query itself also needs support. An engine that understands functions but not the query stays on the baseline. Both paths look the same here, at the cost of maintaining two calculations for now.

Do not use @supports (padding: --space(3)) to prove that the function exists. An engine supporting custom functions may return true even for an undefined name. A parsable declaration does not guarantee successful evaluation. Nor does at-rule() validate individual arguments.

The example results and baseline were checked on October 27, 2026, using local Chromium, Playwright WebKit, and Chromium with CSSFunctions disabled. Playwright WebKit is not stable Safari, and those tests don't establish Firefox support.

I would adopt a function to maintain a calculation jointly, rather than wrap every operation. Two multiplications are already readable as calc(). If the reusable unit is several declarations and nested rules, that is the next question in the @mixin draft; a function's job ends with a value.