Sub topic 9
Writing your own functions
This chapter assumes you can compile SCSS with the Dart Sass command line. It also assumes you know variables and mixins, and that you understand CSS units such as px and rem.
What a function does
A function is a named calculation. It takes arguments, runs some Sass code, and returns one value. The compiler replaces the call with that value, so the CSS output contains only the result.
Mixins differ from functions. A mixin emits CSS rules and declarations. A function never emits CSS. It only produces a value that you use inside a declaration.
A first function
Define a function with @function and send the result back with @return. This one converts pixels to rem.
@use 'sass:math';
@function rem($px, $base: 16) {
@return math.div($px, $base) * 1rem;
}
.title {
font-size: rem(24);
}This compiles to font-size: 1.5rem;. Three details matter here:
$base: 16is a default value, so callers can leave that argument out.math.divdoes the division. The/operator is deprecated for division.- Multiplying a plain number by
1remattaches the unit.
Keyword and variable arguments
You can pass arguments by name. This makes calls easier to read when a function has several parameters.
.small-title {
font-size: rem($px: 20, $base: 10);
}A parameter that ends in ... collects any number of arguments into a list.
@function sum($numbers...) {
$total: 0;
@each $n in $numbers {
$total: $total + $n;
}
@return $total;
}
.box {
width: sum(10px, 20px, 30px);
}The output is width: 60px;.
Conditionals and errors
A function body can use @if and @else, and it can stop compilation with @error. Use this to reject bad input early.
@function spacing($steps, $unit: 8px) {
@if $steps < 0 {
@error "spacing() needs a non-negative number, got #{$steps}.";
}
@return $steps * $unit;
}
.card {
padding: spacing(2);
}Here spacing(2) returns 16px. A call such as spacing(-1) stops the build with your message instead of writing broken CSS.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `Functions must return a value` or empty output | The function has no `@return` on every path. | Add `@return`, including in each `@else` branch. |
| `Undefined function` for `math.div` | The `sass:math` module is not loaded. | Add `@use 'sass:math';` at the top of the file. |
| Deprecation warning about `/` | Division written with the slash operator. | Use `math.div($a, $b)`. |
| Unexpected units such as `px*rem` | Multiplied two values that both have units. | Divide out the unit first, or return a unitless number. |
| Declarations written inside a function | A function cannot emit CSS. | Use a mixin instead. |
When to choose a function
Use a function when you need a value: a size, a color, or a lookup. Use a mixin when you need a block of declarations or rules. The chapter on maps shows functions reading from design tokens, and the chapter on modules shows how to share functions across files.
Exercise
Write a function named em that converts a pixel value to em using a default base of 16. What does em(32) compile to?
Show answer
Use @use 'sass:math'; then @function em($px, $base: 16) { @return math.div($px, $base) * 1em; }. The call em(32) compiles to 2em, because 32 divided by 16 is 2 and the result is multiplied by 1em.