5.3 · Logic in CSS

Your own functions: @function

Write reusable functions that return values, in plain CSS.

@function --space(--n) defines a function you call as --space(4). Inside, result says what it returns. Arguments are custom properties, so var(), calc() and other functions all work.

Parameters and the result can be typed, for example --n <number> and returns <length>. This is the Sass @function idea, but it runs in the browser and reacts to custom properties.

Before
// Sass, at build time
@function space($n) {
  @return $n * 0.25rem;
}
.card { padding: space(6); }
Now
@function --space(--n) {
  result: calc(var(--n) * 0.25rem);
}

.card { padding: --space(6); }