The sass:list module helps you work with lists in Sass easily. It gives you tools to add, remove, or check items in lists without mistakes.
sass:list module
@use "sass:list"; // Example functions: list.append($list, $value, $separator: auto) list.length($list) list.index($list, $value) list.join($list1, $list2, $separator: auto) list.remove($list, $value) list.nth($list, $index)
Always @use "sass:list"; to access these functions.
Lists in Sass can be comma-separated or space-separated; some functions let you control this with $separator.
yellow to the end of the $colors list.@use "sass:list"; $colors: red, green, blue; $new-colors: list.append($colors, yellow); // $new-colors is red, green, blue, yellow
Helvetica in the $fonts list.@use "sass:list"; $fonts: Arial, Helvetica, sans-serif; $index: list.index($fonts, Helvetica); // $index is 2
$spacing list.@use "sass:list"; $spacing: 0.5rem 1rem 2rem; $length: list.length($spacing); // $length is 3
@use "sass:list"; $list1: a, b, c; $list2: d, e; $joined: list.join($list1, $list2); // $joined is a, b, c, d, e
This Sass code shows how to use the sass:list module to get the length of a list, add an item, remove an item, and get an item by position. The results are stored in CSS custom properties so you can see them in the browser.
@use "sass:list"; $breakpoints: mobile, tablet, desktop; // Print original list length $original-length: list.length($breakpoints); // Add a new breakpoint $new-breakpoints: list.append($breakpoints, widescreen); // Remove the breakpoint 'tablet' $updated-breakpoints: list.remove($new-breakpoints, tablet); // Get the first breakpoint $first-breakpoint: list.nth($updated-breakpoints, 1); // Output styles to see results body { --original-length: #{$original-length}; --new-list: #{list.join($new-breakpoints, (), ", ")}; --updated-list: #{list.join($updated-breakpoints, (), ", ")}; --first-breakpoint: #{$first-breakpoint}; }
The list.length() function runs in constant time, so it is very fast.
Remember that list indexes start at 1, not 0.
Use list.remove() carefully because removing an item shifts the positions of later items.
The sass:list module helps manage lists easily in Sass.
You can add, remove, find, and join list items safely.
Always use @use "sass:list"; to access these functions.
Practice
What is the main purpose of the sass:list module in Sass?
Solution
Step 1: Understand the sass:list module role
The sass:list module offers functions to work with lists, such as adding, removing, or finding items.Step 2: Compare options with module purpose
Options A, B, and D describe unrelated tasks like colors, fonts, or compilation, which are not the sass:list module's job.Final Answer:
To provide functions for managing lists like adding or removing items -> Option AQuick Check:
sass:list = list management [OK]
- Confusing sass:list with color or font modules
- Thinking sass:list compiles Sass files
- Assuming sass:list handles CSS properties directly
Which of the following is the correct way to import the sass:list module in your Sass file?
Solution
Step 1: Recall Sass module import syntax
Modern Sass uses@useto import modules, not@importwhich is deprecated.Step 2: Match options with correct syntax
@use "sass:list"; uses@use "sass:list";which is the correct way to import the list module.Final Answer:
@use "sass:list"; -> Option DQuick Check:
Use @use for modules [OK]
- Using @import instead of @use
- Confusing @include with module import
- Using @forward incorrectly for importing
Given the Sass code below, what will be the output of list.length($colors)?
@use "sass:list";
$colors: red, green, blue;
.result {
length: list.length($colors);
}Solution
Step 1: Understand the list variable
The variable$colorscontains three items: red, green, and blue.Step 2: Apply list.length function
Thelist.length()function returns the number of items in the list, which is 3 here.Final Answer:
length: 3; -> Option CQuick Check:
3 items in list = 3 [OK]
- Counting commas instead of items
- Confusing list length with string length
- Forgetting to import sass:list module
Identify the error in the following Sass code using sass:list module:
@use "sass:list";
$items: 1 2 3;
$new-items: list.push($items, 4);
.result {
content: $new-items;
}Solution
Step 1: Check list syntax in $items
The list$itemsis written as1 2 3without commas, which is valid space-separated list.Step 2: Understand list.append behavior
Thesass:listmodule haslist.append()to add items, but nolist.push().Step 3: Identify the error
The code uses non-existentlist.push(), causing a compilation error. Change tolist.append().Final Answer:
Using list.push instead of list.append -> Option AQuick Check:
sass:list.append exists, list.push does not [OK]
- Confusing list.append with list.push (no list.push in sass:list)
- Assuming space-separated lists are always safe
- Ignoring module import correctness
You want to combine two lists $list1: 1, 2, 3; and $list2: 4, 5; into one list using sass:list functions. Which code snippet correctly joins these lists?
@use "sass:list";
// Options:
// A
$combined: list.length($list1) + list.length($list2);
// B
$combined: list.append($list1, $list2);
// C
$combined: list.insert($list1, $list2, 1);
// D
$combined: list.join($list1, $list2, comma);
Solution
Step 1: Understand list.join function
Thelist.join()function combines two lists into one, with a separator (comma or space).Step 2: Analyze each option
$combined: list.join($list1, $list2, comma); correctly useslist.join($list1, $list2, comma)to join lists with commas.
$combined: list.append($list1, $list2); appends the entire second list as one item, not merging.
$combined: list.insert($list1, $list2, 1); inserts the whole second list at position 1, nesting it.
$combined: list.length($list1) + list.length($list2); adds lengths, not lists.Final Answer:
$combined: list.join($list1, $list2, comma); -> Option BQuick Check:
Use list.join to merge lists [OK]
- Using list.append to merge lists (adds as one item)
- Using list.insert to add whole list inside another
- Adding lengths instead of joining lists
Ready to prove it?
Pass the test and this pattern moves unknown
