Sass Lists

Beginner
9 min read
Updated: Nov 2026
5 examples
Dart Sass

Definition and Usage

Lists hold a sequence of values. Separators can be spaces (10px 15px 0 0), commas (Helvetica, Arial, sans-serif), or slashes (via list.slash()). Brackets are optional.

Indexes start at 1.

1 is the first item; -1 is the last. That differs from many languages that start at 0.

Immutable.

list.append() returns a new list. Reassign the variable if you need to accumulate values.

Empty () is not valid CSS.

Sass will not let you put an empty unbracketed list in a property value. Use [] when you need an empty bracketed list for CSS.

Syntax

SCSS
@use "sass:list";

$space: 10px 12px 16px;              // space-separated
$comma: 40px, 50px, 80px;            // comma-separated
$grid: [line1 line2];                // bracketed (handy for Grid)

list.nth($space, 2);                 // 12px
list.append($space, 25px);           // 10px 12px 16px 25px
list.index(1px solid red, solid);    // 2

@each $size in $comma { ... }

Pattern: store a list → read with list.nth / find with list.index → grow with list.append → loop with @each.

Common Patterns

Everyday list tools:

PatternResult idea
a b c / a, b, cSpace- or comma-separated list
[a b]Bracketed list (CSS Grid lines, etc.)
list.nth($list, $n)Element at index (1 or -1)
list.append($list, $val)New list with value at the end
list.index($list, $val)Index, or null if missing
@each $item in $listEmit styles for every element

SCSS Examples

Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).

Example 1 — Official @each Over a List

Generate a rule for every size in a comma-separated list.

SCSS
$sizes: 40px, 50px, 80px;

@each $size in $sizes {
  .icon-#{$size} {
    font-size: $size;
    height: $size;
    width: $size;
  }
}

How It Works

  1. $sizes is a comma-separated list.
  2. @each assigns each value to $size.
  3. #{$size} builds the class name.
  4. Matches the official Sass docs sample.

Example 2 — Official list.nth

Read by index—2 from the start, -1 from the end.

SCSS
@use "sass:list";

.demo {
  --second: #{list.nth(10px 12px 16px, 2)};
  --last: #{list.nth([line1, line2, line3], -1)};
}

How It Works

  1. Index 2 is the second space-separated value.
  2. -1 is the last bracketed item.
  3. Indexes are 1-based.
  4. Matches the official list.nth sample.

Example 3 — Official list.append

Return a new list with a value added at the end.

SCSS
@use "sass:list";

.demo {
  --space: #{list.append(10px 12px 16px, 25px)};
  --bracket: #{list.append([col1-line1], col1-line2)};
}

How It Works

  1. append does not mutate the original list.
  2. Separator usually matches the input list.
  3. Brackets stay when the input was bracketed.
  4. Matches the official append sample idea.

Example 4 — Official list.index Validation

null is falsey—reject invalid values with @if not list.index(...).

SCSS
@use "sass:list";

$valid-sides: top, bottom, left, right;

@mixin attach($side) {
  @if not list.index($valid-sides, $side) {
    @error "#{$side} is not a valid side.";
  }
  border-#{$side}: 2px solid;
}

.box {
  @include attach(top);
}

How It Works

  1. list.index finds top at index 1.
  2. Missing values return null (falsey).
  3. @error stops bad arguments early.
  4. Matches the official validation sample idea.

Example 5 — Official Argument Lists + Keywords

$args... is a special list; keywords become a map via meta.keywords.

SCSS
@use "sass:meta";

@mixin syntax-colors($args...) {
  @each $name, $color in meta.keywords($args) {
    pre span.stx-#{$name} {
      color: $color;
    }
  }
}

@include syntax-colors(
  $string: #080,
  $comment: #800,
  $variable: #60b,
);

How It Works

  1. $args... collects arbitrary arguments.
  2. meta.keywords turns named args into a map.
  3. @each loops key/value pairs.
  4. Matches the official argument-list sample.

Edge Cases & Pitfalls

Most list bugs are 0-based thinking or mutating expectations.

1-based

First item is index 1

list.nth($list, 0) is invalid. Use 1 or -1.

immutable

append does not change the original

Write $list: list.append($list, $val); to keep the new value.

/

Slash lists need list.slash()

You cannot write slash-separated lists literally yet. See list.slash().

()

Empty () cannot be a CSS value

Use bracketed [] when an empty list must appear in CSS.

Sass Support

Lists and sass:list are core Sass on Dart Sass.

FeatureStatus
Space / comma listsSupported
Bracketed lists []Supported (LibSass/Ruby 3.5+)
Slash-separated listsVia list.slash()
@each / argument listsSupported

Compile with the Dart Sass CLI.

Key Takeaways

  • Lists are space- or comma-separated sequences (brackets optional).
  • Indexes start at 1; -1 is the last item.
  • @each is the usual way to generate CSS from a list.
  • Immutable—reassign after append.

One line: store related values in a list, then loop or look them up with sass:list helpers.

Compile Notes

Practical checklist:

checklist.txt
1. Pick space or comma separators (keep them consistent)
2. Remember indexes start at 1 (-1 = last)
3. Use @each to generate repeated CSS
4. Use list.nth / list.index / list.append from sass:list
5. Reassign after append—lists are immutable

Frequently Asked Questions

A list is a sequence of values. Elements can be separated by spaces, commas, or slashes (slash lists are created with list.slash()). Brackets like [a b] are optional but useful for CSS Grid.
No. Index 1 is the first element. Negative indexes count from the end: -1 is the last element.
No. Lists are immutable. Functions like list.append() return a new list; they do not change the original. Reassign the variable if you need to update state.
Use @each $item in $list { … }. Each element is assigned to $item once per iteration.
It returns null, which is falsey—so you can use it with @if to validate allowed values.
When a mixin or function uses $args..., the collected arguments form a special list. Keyword arguments are available via meta.keywords($args).

Next: Sass Maps

Store key/value pairs—often alongside lists for config and themes.

Maps →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful