SCSS built-in functions
🎯 Introduction
By the end of this article, you will be able to use built-in functions in SCSS to write more flexible, efficient styles. Avoiding deprecated or discouraged functions helps improve style management and reduce compatibility and maintenance problems.
🌟 Introducing built-in functions
SCSS is a syntax for Sass, which provides many built-in functions for colors, strings, lists, and mathematics. Functions such as lighten() and darken() were commonly used in older code, but modern Sass favors more capable color adjustment functions.
This article focuses on modern, recommended built-in functions and how to use them.
🎨 Color functions
Rather than using lighten($color, $amount) and darken($color, $amount) to change lightness, prefer the more flexible color.adjust() and color.scale().
color.adjust()
This function gives you fine-grained control over color channels, including red, green, blue, alpha, lightness, and saturation.
$primary: #3498db;
// Increase lightness by 20 percentage points
$brighter: color.adjust($primary, $lightness: 20%);
// Decrease saturation by 10 percentage points
$less-saturated: color.adjust($primary, $saturation: -10%);
color.scale()
This function scales color channels proportionally, for example to increase saturation or make a color more transparent.
$primary: #3498db;
// Scale saturation up by 30%
$more-vibrant: color.scale($primary, $saturation: 30%);
// Scale alpha down by 50%
$less-transparent: color.scale($primary, $alpha: -50%);
color.adjust() and color.scale() over legacy lighten() and darken() for more precise, flexible control and to avoid unwanted color changes.🧮 Math functions
Sass provides math functions such as math.div(). Using / for Sass division is deprecated; use math.div() instead.
$width: 100px;
// Calculate half the width
$half-width: math.div($width, 2);
Load the math module at the top of the file:
@use 'sass:math';
This syntax keeps mathematical division distinct from CSS slash separators.
🧵 String functions
The following functions are useful for working with strings in SCSS:
string.index($string, $substring)returns the position of the first occurrence of a substringstring.length($string)returns the string lengthstring.slice($string, $start-at, $end-at: null)extracts part of a string
@use 'sass:string';
$txt: 'Hello, SCSS!';
// Find the position of "SCSS" in the string
$pos: string.index($txt, 'SCSS');
// Extract "Hello"
$hello: string.slice($txt, 1, 5);
🧩 List functions
Use these functions to work with lists:
list.length($list)returns the list lengthlist.nth($list, $n)returns the nth itemlist.append($list, $val)appends an item
@use 'sass:list';
$colors: (red, green, blue);
// Get the length
$len: list.length($colors);
// Get the second color
$second: list.nth($colors, 2);
// Append a color
$new-list: list.append($colors, yellow);
🧙♂️ Other useful functions
type-of($value)returns the value's type, which is useful for debugging variablesunit($number)returns the unitunitless($number)checks whether a number has no unit
$value: 15px;
$type: type-of($value); // Returns "number"
$unit: unit($value); // Returns "px"
$is-unitless: unitless($value); // false
lighten() and darken(), and avoid using / for Sass division. These forms are deprecated or may produce errors depending on the Sass version. Update your project to preserve compatibility and simplify future maintenance.❓ Questions and answers
Q1: Why are the old lighten() and darken() functions discouraged?
Their adjustment model is more limited. color.adjust() provides more flexible control over individual channels, giving you greater control over the result.
Q2: Why should I avoid / for division?
Sass is moving toward interpreting / as a CSS slash separator, which can cause errors in code that expects division. Use math.div() for mathematical division.
🧪 Exercises
- Use
color.adjust()to adjust a blue color, reducing its alpha by half and increasing its lightness by 10 percentage points.
@use 'sass:color';
$base: #0000ff;
$adjusted: color.adjust($base, $alpha: -50%, $lightness: 10%);
// Test the resulting color
.test {
background-color: $adjusted;
}
- Write a function that accepts a string, returns its length, and extracts its first half.
@use 'sass:string';
@mixin string-info($str) {
$len: string.length($str);
$half: math.div($len, 2);
$half-str: string.slice($str, 1, $half);
content: 'Length: #{$len}, Half: #{$half-str}';
}
.test-string::before {
@include string-info('Hello SCSS Functions');
}
🎉 You now know how to use built-in functions for color adjustments, mathematics, strings, and lists! Try applying them to older code in your projects to make styles more modern and maintainable!
