Workflow Intermediate

Reusable CSS logic without Sass mixins

Reusable calculations and logic in stylesheets used to require Sass or other preprocessors. Native CSS @function lets you define custom functions that return computed values, right in your stylesheet.

Old 8 lines
1// Sass / SCSS
2@function fluid($min, $max) {
3  @return clamp(
4    #{$min},
5    #{$min} + (#{$max} - #{$min}) * ...,
6    #{$max}
7  );
8}
9
10/* Requires build step to compile */
Modern
8 lines
1@function --fluid(
2  --min, --max
3) {
4  @return clamp(
5    var(--min),
6    50vi,
7    var(--max)
8  );
9}
CSS function computing fluid sizes
SASS (compile-time)
@function fluid($min, $max) { ... }
Returns static value, can't use vw/vh
CSS (runtime)
@function --fluid(--min, --max) { ... }
Runs in browser, uses any CSS unit

Fluid heading

font-size: --fluid(1.2rem, 2.4rem)

No build step

Native CSS functions run in the browser. No Sass compiler, no PostCSS plugin, no build pipeline required.

Runtime computed

Unlike Sass which compiles to static values, CSS @function can use runtime values like viewport units, custom properties, and env().

Composable

Functions can call other functions and use custom properties. Build complex design token systems with pure CSS.

Browser Support
40%
Chrome Edge Firefox 🔜
View on caniuse.com →
Build Step
None
Runs in browser
Old Approach
Sass functions
Requires compiler
Modern Approach
@function
Native CSS

How it works

Preprocessors like Sass introduced functions decades ago, allowing developers to encapsulate reusable calculations. But Sass functions compile to static values at build time — they can't react to runtime conditions like viewport size changes or user preferences.

CSS @function brings this capability natively to the browser. You define a function with @function --name(--param) { @return ... } and call it anywhere a value is expected: font-size: --fluid(1rem, 2rem). Because it runs at runtime, it can use viewport units, custom properties, and other dynamic values that Sass simply can't access.

ESC