Sass Functions, Built-in Modules, and Calculations
Create custom functions, use modern Sass modules, manipulate colors and lists, perform calculations, and distinguish Sass computations from browser calculations.
Writing Custom Functions
Sass functions encapsulate calculations and return values using @return. They are suitable for calculations that should be reused in multiple declarations or components.
A function should have a clear contract: its arguments represent inputs, and its returned value represents the result. Keep functions focused and avoid using them when a direct CSS value is easier to understand.
Sass functions execute during compilation. They cannot inspect the browser viewport, query the DOM, or retrieve a runtime CSS custom property's computed value.
For browser-dependent calculations, emit a CSS calculation expression rather than trying to evaluate browser state during Sass compilation.
@function spacing($step, $base: 0.25rem) {
@return $step * $base;
}
.card {
padding: spacing(4);
gap: spacing(2);
}
.section {
margin-block: spacing(8);
}spacing(4) produces 1rem, while spacing(2) produces 0.5rem.
A spacing function can help enforce a consistent design scale. In a larger design system, you may also validate inputs or use a map to restrict values to an approved token set.Which directive returns a calculated value from a Sass function?
Built-in Modules: sass:math, sass:color, sass:list, and sass:map
Modern Sass organizes many built-in functions into modules. Use @use to access these APIs through explicit namespaces.
Important modules include:
sass:mathfor mathematical functions and unit-related operations.sass:colorfor color manipulation and color-space operations.sass:listfor working with ordered collections.sass:mapfor key-value collections.sass:stringfor string operations.sass:metafor introspection and type information.
Namespaced APIs improve clarity and reduce ambiguity. For example, math.div(12px, 3) communicates that a mathematical division function is being used.
Legacy global built-in function calls may still appear in older code, but new projects should favor the module system and consult the installed Dart Sass documentation for supported APIs.
Use namespaced built-in functions such as math.div() and map.get() in modern Sass. Verify the availability and argument syntax of individual APIs in the Dart Sass documentation.
@use "sass:math";
@use "sass:color";
@use "sass:list";
@use "sass:map";
$colors: (
primary: #5b5bd6,
danger: #c62828
);
$primary: map.get($colors, primary);
$spacing: math.div(24px, 2);
$first-color: list.nth((red, green, blue), 1);
$muted-primary: color.adjust($primary, $lightness: 10%);
.card {
color: $primary;
padding: $spacing;
border-color: $muted-primary;
}
.label {
content: $first-color;
}content declaration is included only to demonstrate the resulting Sass value and is not a recommended way to display user-facing text. In application styles, use these computed values in appropriate visual declarations.Which module provides the `map.get()` function?
Color Manipulation and Theme Calculations
Sass provides color functions for modifying and inspecting colors. The sass:color module offers functions for operations such as adjusting channels, changing color properties, mixing colors, and working with color spaces.
Color manipulation is useful when building related theme colors, hover states, muted borders, and accessible component palettes. However, a mathematically lighter color is not necessarily accessible against every background.
Always validate text contrast and visual state distinctions. For production design systems, explicitly defined semantic tokens are often easier to audit than a large number of implicit color transformations.
@use "sass:color";
$brand: #5b5bd6;
$brand-hover: color.adjust($brand, $lightness: -8%);
$brand-soft: color.mix(white, $brand, 85%);
.button {
background: $brand;
&:hover {
background: $brand-hover;
}
}
.badge {
color: $brand;
background: $brand-soft;
}color.adjust() changes the selected color property, while color.mix() blends two colors.
The generated colors should be tested against the intended text and background combinations. For accessible interfaces, verify contrast ratios and do not rely on color alone to communicate state.Why should generated color variations still undergo accessibility testing?
Sass Mathematics Versus CSS calc(), min(), max(), and clamp()
Sass calculations happen during compilation when the operands can be resolved by Sass. CSS calculations are evaluated by the browser and can use runtime values such as percentages, viewport dimensions, and custom properties.
Use Sass math to derive fixed values from known tokens. Use calc() to combine values that depend on the rendered layout. Use clamp() when a value should grow within a minimum and maximum range, such as fluid typography.
For example, font-size: clamp(1rem, 2vw, 2rem) allows text to scale with viewport width while remaining within the specified limits.
Do not force Sass to calculate values that depend on information unavailable until the browser performs layout.
@use "sass:math";
$base-font: 1rem;
$heading-scale: 1.5;
.card-title {
font-size: $base-font * $heading-scale;
}
.hero-title {
font-size: clamp(2rem, calc(1.2rem + 3vw), 4rem);
}
.content {
width: calc(100% - 2rem);
max-width: 72rem;
margin-inline: auto;
}clamp() and calc() so its size responds to viewport width. The content width also depends on the available layout width.
This combination uses each calculation system for its intended purpose.Which approach is best when a font size must respond dynamically to viewport width while remaining bounded?