Sass Lists
A list is an ordered sequence of values. You use lists all the time in CSS without thinking about it: margin: 4px 8px is a space-separated list, and font-family: Inter, sans-serif is a comma-separated one. Sass lets you store, inspect and build lists with the sass:list module.
Writing lists
$spacing: 4px 8px 16px 32px; // space-separated
$fonts: "Inter", system-ui, sans-serif; // comma-separated
$pairs: (1px solid) (2px dashed); // list of lists
$slash: list.slash(16px, 1.5); // slash-separated (font: 16px/1.5)
$brackets: [main-start]; // bracketed (for grid line names)Indexes in Sass start at 1, not 0. Negative indexes count from the end: -1 is the last item.
Reading lists
@use "sass:list";
$spacing: 4px 8px 16px 32px;
.card {
padding: list.nth($spacing, 3);
margin-bottom: list.nth($spacing, -1);
--count: #{list.length($spacing)};
--pos-of-16: #{list.index($spacing, 16px)};
}.card {
padding: 16px;
margin-bottom: 32px;
--count: 4;
--pos-of-16: 3;
}
The sass:list functions
| Function | Result |
|---|---|
list.length($list) |
Number of items |
list.nth($list, $n) |
Item at position $n (1-based) |
list.index($list, $value) |
Position of $value, or null |
list.append($list, $value) |
New list with one item added |
list.join($list1, $list2) |
New list combining both |
list.set-nth($list, $n, $value) |
New list with one item replaced |
list.separator($list) |
space, comma or slash |
list.zip($lists...) |
Pairs up items from several lists |
Like maps, lists are immutable: these functions return new lists.
Building lists
@use "sass:list";
$shadows: ();
$shadows: list.append($shadows, 0 1px 2px rgb(0 0 0 / 0.2), $separator: comma);
$shadows: list.append($shadows, 0 4px 12px rgb(0 0 0 / 0.1), $separator: comma);
$base-fonts: system-ui, sans-serif;
$heading-fonts: list.join("Playfair Display", $base-fonts);
.panel {
box-shadow: $shadows;
}
h1 {
font-family: $heading-fonts;
}.panel {
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2), 0 4px 12px rgba(0, 0, 0, 0.1);
}
h1 {
font-family: "Playfair Display", system-ui, sans-serif;
}
list.join keeps the separator of its first list. A single value like "Playfair Display" has no separator of its own, so the result took the comma from $base-fonts. You can force one with $separator: comma.
Looping over a list
$sides: top, right, bottom, left;
.debug-box {
@each $side in $sides {
border-#{$side}: 2px dashed hotpink;
}
}.debug-box {
border-top: 2px dashed hotpink;
border-right: 2px dashed hotpink;
border-bottom: 2px dashed hotpink;
border-left: 2px dashed hotpink;
}
Zipping lists together
list.zip combines several lists into a list of tuples, which @each can unpack:
@use "sass:list";
$names: primary, accent, muted;
$values: #cc6699, #ffb703, #8a8a8a;
@each $name, $value in list.zip($names, $values) {
.border-#{$name} {
border-color: $value;
}
}.border-primary {
border-color: #cc6699;
}
.border-accent {
border-color: #ffb703;
}
.border-muted {
border-color: #8a8a8a;
}
Tip: If you find yourself zipping two lists, a map is usually the clearer data structure:
(primary: #cc6699, accent: #ffb703).
Lists vs maps
| List | Map | |
|---|---|---|
| Access by | Position (list.nth) |
Key (map.get) |
| Order | Ordered | Ordered (insertion order) |
| Good for | CSS values (shadows, font stacks), simple sequences | Named tokens (palettes, breakpoints) |
Test yourself
What does list.nth(10px 20px 30px, 1) return?
- 20px
- 10px
- 30px
- An error — indexes start at 0
What does list.nth($list, -1) return?
- The first item
- The last item
- null
- An error
Which separator does Inter, sans-serif use?
- space
- comma
- slash
- none