CSS Custom Functions: Reuse Design Logic Without Sass


CSS custom functions bring parameterised, reusable value logic to native CSS. The new @function rule can reduce repeated clamp(), colour, and spacing expressions without moving every design decision into Sass or JavaScript. It is a promising progressive-enhancement tool today—not a reason to remove a production fallback while browser support remains limited.

TL;DR: CSS @function lets a stylesheet define a dashed function, accept custom-property arguments, and return a CSS value. Use it first for small, repeated value calculations in Chrome-family enhancement layers; retain ordinary declarations as the cross-browser baseline. The feature is specified by the CSSWG and implemented in Chromium, but it is not yet Baseline across major browsers.

Table of contents

  1. What problem do CSS custom functions solve?
  2. How does @function work?
  3. Which design-system logic should become a function?
  4. Can custom functions replace Sass or utility classes?
  5. How should teams ship @function safely?
  6. What are the limits today?

What problem do CSS custom functions solve?

Custom properties solved named values: put --space-3: 0.75rem on a token layer and reuse it everywhere. They do not, by themselves, solve named calculations. A component library soon accumulates the same clamp(), color-mix(), and calc() patterns in dozens of selectors. The values may be consistent, but the rule that produces them has no single name or place to change.

CSS custom functions add that missing abstraction. A function has a dashed name, optional inputs, optional types, and a result. It is used as a value anywhere a compatible CSS value is accepted. The CSS Functions and Mixins specification describes them as advanced custom properties whose substitution can depend on arguments, local variables, and conditional rules.

That makes the feature interesting now. Native functions sit between two existing extremes:

  • Copy a long expression into each component, which makes a token change risky.
  • Put a small presentation decision in JavaScript, which adds runtime work and can obscure the cascade.

Recent CSS additions have already made stylesheets more expressive: CSS if(), native contrast-color(), and CSS @scope each remove a different category of glue code. @function is the reuse layer for the value logic those features enable.

How does @function work?

Define a function with @function, name it with a -- prefix, declare parameters, and set its result. Invoke it with parentheses, unlike var(). Chromium's modern web guidance uses this model to remove repeated design-system calculations.

Here is a compact type-scale helper. It accepts the smallest and largest permitted font size, then returns a fluid value constrained between them:

css
@function --fluid-type(
  --min <length>,
  --max <length>
) returns <length> {
  result: clamp(var(--min), 1rem + 2vw, var(--max));
}

.hero-title {
  font-size: --fluid-type(2rem, 4.5rem);
}

.section-title {
  font-size: --fluid-type(1.5rem, 2.5rem);
}

The types are valuable even in a language famous for flexible values. returns <length> documents the contract and gives the browser an opportunity to reject an invalid call instead of letting an inappropriate value drift through a component. The current MDN @function reference documents the parameter grammar, return types, defaults, and the fact that the feature is experimental and of limited availability.

The function itself is declarative. There is no DOM access, loop, fetch, or JavaScript-style early return. It calculates a value from CSS inputs and the current cascade. That restriction is a strength: the output stays inspectable in DevTools and the browser continues to own style evaluation.

A practical colour example

The same approach helps where a system repeats a policy rather than a literal token. This function produces a muted version of any supplied foreground colour:

css
@function --muted(--color <color>, --amount <percentage>: 32%) returns <color> {
  result: color-mix(in oklab, var(--color), transparent var(--amount));
}

.card-meta {
  color: rgb(25 35 50);
  color: --muted(rgb(25 35 50));
}

The first declaration is not accidental. It is a usable baseline for browsers that do not understand the second one. Supporting browsers apply the later declaration; other browsers discard only the unknown value and retain the ordinary colour. This is the same resilient mindset that makes CSS scroll-state queries and scroll-driven animations safe to introduce incrementally.

Which design-system logic should become a function?

The best candidates already meet three tests: the expression is repeated, its inputs have clear names, and a future maintainer benefits from seeing the policy rather than the arithmetic. Good early targets include:

  • fluid type, spacing, and gutter calculations;
  • colour transformations such as muted, elevated, or translucent variants;
  • dimension guards such as a consistent readable measure;
  • token normalisation, for example turning a component's density into a gap;
  • responsive values whose rule is identical but endpoints differ.

For example, a layout utility can name the reading-width rule rather than reproducing it in every prose component:

css
@function --readable-width(--page-gutter <length>) returns <length> {
  result: min(72ch, calc(100% - (2 * var(--page-gutter))));
}

.article,
.docs-page {
  inline-size: --readable-width(1.25rem);
  margin-inline: auto;
}

Avoid turning every one-off value into a function. --radius: 0.75rem remains a good custom property, and padding: 1rem needs no abstraction. A function should make the stylesheet easier to read at the call site. If --something() hides a single obvious value or grows a dozen parameters, it has become a miniature programming language rather than a design token.

Can custom functions replace Sass or utility classes?

No—and that is the wrong migration goal. Sass can generate selectors, iterate over maps, split partials, and compile across a whole codebase. Utility systems establish composition conventions and constraints. Native custom functions operate at computed-value time inside CSS declarations. They improve a specific kind of repetition; they do not replace build-time generation or a team’s component API.

The useful shift is architectural. Keep preprocessing if it delivers value, but stop treating it as the only home for reusable value logic. A browser-native function can travel with a component stylesheet, participate in cascade layers, and remain readable after deployment. The specification also says that, when identically named functions compete, the stronger cascade layer wins—another reason to give functions a clear owner, such as a tokens or utilities layer.

css
@layer tokens, components;

@layer tokens {
  @function --control-height(--size <length>) returns <length> {
    result: max(2.75rem, calc(var(--size) * 2.5));
  }
}

@layer components {
  .button {
    min-block-size: --control-height(1rem);
  }
}

Do not use a custom function to encode application state. CSS variables can reflect state supplied by a class, attribute, container query, or media query, but authentication, pricing, permissions, and data fetching still belong in application code. Keeping that boundary firm makes the CSS layer predictable.

How should teams ship @function safely?

Treat custom functions as a progressive enhancement until the compatibility picture changes. The Web Platform DX feature data currently marks @function as limited availability: Chromium support does not make a feature portable to every browser your customers use.

  1. Start with a baseline declaration. Put an ordinary, supported value first. Add the function-powered value after it, as in the muted-colour example.
  2. Choose non-critical enhancements. Fluid polish, supplementary spacing, and decorative treatments are safer than a required layout or contrast rule.
  3. Test the fallback deliberately. Use a browser without support or temporarily disable the enhanced declaration. “The page loads” is not enough; verify readability, tap targets, overflow, and high-contrast behaviour.
  4. Centralise only stable policies. Put shared functions in a documented layer and review them like API changes. A renamed parameter can affect every consumer.
  5. Watch compatibility, then simplify. When support reaches the browsers your product actually supports, remove duplicate declarations in a planned cleanup—not on the day an article says the feature is exciting.

The code below shows a conservative pattern for a responsive gap. The fallback is intentionally complete; users do not need support for @function to receive a coherent layout.

css
.feature-grid {
  display: grid;
  gap: clamp(1rem, 2vw, 2rem); /* baseline */
}

@function --content-gap(--min <length>, --max <length>) returns <length> {
  result: clamp(var(--min), 2vw, var(--max));
}

.feature-grid {
  gap: --content-gap(1rem, 2rem); /* enhancement */
}

What are the limits today?

Three constraints deserve emphasis. First, support is uneven, so functions cannot be the only route to a necessary value. Second, the related @mixin and @apply ideas are not the same maturity story: MDN notes that browsers currently support custom functions, not the proposed mixin facilities. Do not assume a Sass-like block-reuse syntax has landed because @function has.

Third, custom functions do not magically make CSS dependencies simple. A function can reference custom properties and conditions, so over-nesting can make a style harder to trace. Prefer short functions with explicit inputs; inspect the computed result in DevTools; and document the output type and fallback near the definition.

For component teams, the opportunity is real but modest: fewer duplicated calculations, a more expressive token layer, and less temptation to use JavaScript for presentation-only math. Start with one proven repetition, measure whether the call sites got clearer, and let compatibility—not enthusiasm—set the rollout pace.

FAQ

Are CSS custom functions ready for every production website?

No. They can be used as progressive enhancements, but their limited cross-browser support means a standard CSS fallback is still required for production features that matter to every visitor.

What is the difference between var() and a custom CSS function?

var() substitutes a custom property value. A custom function is invoked with --name() and can accept arguments, use local logic, and return a calculated value.

Do CSS custom functions replace Sass mixins?

No. They reuse CSS values at runtime; Sass mixins and other build tools can generate rules and selectors at build time. The two can coexist.

How can I check browser support for @function?

Consult the MDN compatibility table and your product’s supported-browser policy. Test a real fallback in browsers without support before depending on the feature.

Frequently Asked Questions

Are CSS custom functions ready for every production website?

No. Use them with a standard CSS fallback while browser support remains limited.

What is the difference between var() and a custom CSS function?

var() substitutes a custom property; custom functions accept arguments and return a calculated CSS value.

Do CSS custom functions replace Sass mixins?

No. They provide runtime value reuse, while Sass can generate rules and selectors at build time.