Chapter 12: Sass List
Sass List Functions — the tools from the sass:list module. Lists are one of Sass’s most powerful data structures, and mastering list functions lets you build flexible, scalable design systems (think spacing scales, breakpoints, color palettes, component variants, etc.).
In 2026 (Dart Sass ~1.97+), you must load the module explicitly with @use ‘sass:list’; — old global names like append() still work but throw deprecation warnings in recent versions. We’ll use the modern module style in every example.
1. Quick Refresher: What is a List in Sass?
A list is an ordered collection of values (separated by spaces or commas, optionally wrapped in () or []).
|
0 1 2 3 4 5 6 7 8 9 10 |
$colors: #ff0000 #00ff00 #0000ff; // space-separated $sizes: (small, medium, large); // comma-separated $breakpoints: 576px 768px 992px 1200px; // spaces ok $shadows: 0 4px 12px rgba(black, 0.08), // multi-value with comma 0 8px 24px rgba(black, 0.12); |
- Lists can be nested (lists of lists = maps alternative)
- They can have separators: space (default for most), comma, or none (slash-separated rare)
- Bracketed or unbracketed — matters for output in some cases
All list functions return new lists — Sass lists are immutable (great for avoiding bugs).
2. Loading the Module (Always!)
|
0 1 2 3 4 5 6 7 |
@use 'sass:list' as l; // alias 'l' is common & short // or @use 'sass:list'; → list.nth(...) |
3. The Core sass:list Functions (2026 – Full Practical Set)
Here are the most useful ones with examples and real-world use cases.
A. list.length($list) / length($list)
→ Number of items in the list
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@use 'sass:list' as l; $themes: light dark high-contrast; @debug l.length($themes); // 3 @if l.length($themes) > 2 { .app { --theme-count: #{l.length($themes)}; } } |
Use case: Loop limits, validation, dynamic grid columns.
B. list.nth($list, $n) / nth($list, $n)
→ Get item at 1-based index (negative from end)
|
0 1 2 3 4 5 6 7 8 |
@debug l.nth(10px 20px 30px, 2); // 20px @debug l.nth( (a b c), -1 ); // c @debug l.nth(1px solid red, 3); // red |
Use case: Access specific breakpoint, color shade, etc.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$breakpoints: 576px 768px 992px 1200px; @mixin bp($name) { $index: l.index($breakpoints-names, $name); @if $index { @media (min-width: l.nth($breakpoints, $index)) { @content; } } } |
C. list.index($list, $value) / index($list, $value)
→ 1-based position or null if not found
|
0 1 2 3 4 5 6 7 |
@debug l.index( (success warning danger), warning ); // 2 @debug l.index(1px solid red, dashed); // null |
Use case: Check existence before nth, validate variant names.
D. list.append($list, $val, $separator: auto) / append(…)
→ New list with $val added at the end
|
0 1 2 3 4 5 6 7 |
@debug l.append( (blue red), green ); // blue red green @debug l.append( (blue, red), green, $separator: comma ); // blue, red, green |
Use case: Dynamically build palettes, add modifiers.
|
0 1 2 3 4 5 6 7 |
$base-colors: primary secondary; $all-colors: l.append($base-colors, accent); |
E. list.prepend($list, $val, $separator: auto)
→ New list with $val added at the beginning
|
0 1 2 3 4 5 6 |
@debug l.prepend( (red green), blue ); // blue red green |
Use case: Add default/fallback first.
F. list.join($list1, $list2, $separator: auto, $bracketed: auto)
→ Merge two lists
|
0 1 2 3 4 5 6 7 |
@debug l.join( (1px 2px), (3px 4px) ); // 1px 2px 3px 4px @debug l.join( (a b), (c d), $separator: comma ); // a, b, c, d |
Use case: Combine spacing scales, shadows, transitions.
|
0 1 2 3 4 5 6 7 8 9 |
$shadow-sm: 0 1px 3px rgba(black, 0.12); $shadow-lg: 0 10px 30px rgba(black, 0.15); $all-shadows: l.join($shadow-sm, $shadow-lg); |
G. list.remove($list, $val, $separator: auto)
→ New list without first occurrence of $val
|
0 1 2 3 4 5 6 |
@debug l.remove( (1 2 3 2 4), 2 ); // 1 3 2 4 |
Use case: Clean up duplicate values, remove unwanted variants.
H. list.slice($list, $start-at: 1, $end-at: -1)
→ Sub-list from start to end (negative from end)
|
0 1 2 3 4 5 6 7 |
@debug l.slice( (a b c d e), 2, 4 ); // b c d @debug l.slice( (1 2 3 4), -2 ); // 3 4 |
Use case: Paginate themes, get subset of breakpoints.
I. list.zip($lists…)
→ Combine multiple lists into list of sub-lists (like zip in Python)
|
0 1 2 3 4 5 6 7 |
@debug l.zip( (1 2 3), (a b c), (x y z) ); // (1 a x), (2 b y), (3 c z) |
Use case: Pair colors with names, sizes with icons.
|
0 1 2 3 4 5 6 7 8 9 |
$variants: primary secondary success; $colors: #6c5ce7 #00b894 #00cec9; $theme-map: l.zip($variants, $colors); |
J. list.is-bracketed($list) / is-bracketed($list)
→ true if list uses [] brackets
|
0 1 2 3 4 5 6 7 |
@debug l.is-bracketed([a b c]); // true @debug l.is-bracketed(a b c); // false |
Use case: Control output format for CSS grid-template-areas, etc.
K. list.separator($list) / separator($list)
→ Returns “space”, “comma”, or “slash”
|
0 1 2 3 4 5 6 7 |
@debug list.separator(1px 2px); // space @debug list.separator(1px, 2px); // comma |
Use case: Preserve separator when joining.
4. Real-World Example – Dynamic Variant Classes
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
@use 'sass:list' as l; $variants: primary secondary danger warning info; $colors: #6c5ce7 #00b894 #e74c3c #f1c40f #3498db; @each $variant, $color in l.zip($variants, $colors) { .btn--#{$variant} { background: $color; color: white; &:hover { background: color.adjust($color, $lightness: -12%); } } } |
→ Generates .btn–primary, .btn–secondary, etc. automatically.
5. Quick Tips & Gotchas (2026)
- Indexes are 1-based (not 0-based)
- Negative indexes count from the end (-1 = last item)
- Single values are treated as 1-item lists by most functions
- Use () for comma-separated, space for most CSS values
- For very large lists → performance can matter (rare in Sass)
- Combine with @each loops + maps for powerful systems
- Deprecations: old globals → always use module namespace now
You’ve now unlocked list superpowers! Lists + maps + loops + functions = the foundation of almost every serious Sass design system.
Next — want to build a complete spacing / typography scale using lists + math + custom functions? Or combine lists with maps for full theme objects? Or go into sass:map functions next?
Just tell me — we’re deep in the good stuff now! 🚀
