Sass @function
A function computes and returns a single value. Where a mixin outputs declarations, a function outputs a value that you use inside a declaration — like rem(24px) or spacing(3). Define one with @function and return a result with @return.
@function name($arguments) {
@return <value>;
}Your first function
@use "sass:math";
@function rem($px, $root: 16px) {
@return math.div($px, $root) * 1rem;
}
html { font-size: 100%; }
h2 { font-size: rem(28px); margin-bottom: rem(12px); }
.small { font-size: rem(14px); }html {
font-size: 100%;
}
h2 {
font-size: 1.75rem;
margin-bottom: 0.75rem;
}
.small {
font-size: 0.875rem;
}
Functions are called by name, with arguments in parentheses, anywhere a value is expected.
Arguments
Functions take the same kinds of arguments as mixins:
| Feature | Example |
|---|---|
| Default value | @function rem($px, $root: 16px) |
| Keyword call | rem(24px, $root: 10px) |
| Variable arguments | @function sum($numbers...) |
@function sum($numbers...) {
$total: 0;
@each $n in $numbers {
$total: $total + $n;
}
@return $total;
}
.toolbar {
height: sum(8px, 24px, 8px);
width: sum(120px, 16px, 120px, 16px, 120px);
}.toolbar {
height: 40px;
width: 392px;
}
A spacing system function
Functions make design tokens easy to use consistently:
$space-unit: 4px;
@function space($steps) {
@if $steps < 0 {
@error "space() expects a positive number, got #{$steps}";
}
@return $steps * $space-unit;
}
.card {
padding: space(4) space(5);
margin-bottom: space(6);
gap: space(2);
}.card {
padding: 16px 20px;
margin-bottom: 24px;
gap: 8px;
}
@error stops compilation with a helpful message if someone passes a nonsense value — you'll learn more in the @debug, @warn & @error lesson.
Functions with logic
A function can contain variables, @if, loops — anything except output CSS. Here a function returns the right contrast colour for a background:
@use "sass:color";
@function contrast($bg, $light: white, $dark: #222) {
$l: color.channel($bg, "lightness", $space: hsl);
@return if-dark($l, $light, $dark);
}
@function if-dark($lightness, $light, $dark) {
@if $lightness < 55% {
@return $light;
}
@return $dark;
}
.tag-plum { background: #6a1b9a; color: contrast(#6a1b9a); }
.tag-butter { background: #fff59d; color: contrast(#fff59d); }.tag-plum {
background: #6a1b9a;
color: white;
}
.tag-butter {
background: #fff59d;
color: #222;
}
Notice that a function can call another function, even one defined later in the file.
Mixin or function?
| Use a… | When you want to produce… | Example |
|---|---|---|
@function |
One value | width: col-width(3); |
@mixin |
Declarations or rules | @include truncate; |
Rules and tips
- A function must reach an
@return; a function that ends without returning is an error. - Functions can't contain declarations or style rules.
- Avoid names that clash with CSS functions such as
calc,min,max,clamporvar. - Prefer a namespaced built-in (
math.div,color.adjust) to writing your own when one exists.
Tip: Put shared functions in a partial like
_functions.scssand load them with@use "functions" as fn;→fn.rem(24px).
Test yourself
What keyword sends a value back from a Sass function?
- @return
- return
- @output
- @yield
Which statement is true?
- Functions can output CSS rules
- Mixins return a single value
- Functions return a value; mixins output declarations
- They are interchangeable
What does space(3) return if $space-unit is 4px and space() multiplies by it?
- 3px
- 7px
- 12px
- 34px