Sass list.nth()

Beginner
12 min read
Updated: Nov 2026
5 examples
Dart Sass 1.23+

Definition and Usage

list.nth($list, $n) returns the element of $list at index $n. Indexes are 1-based. If $n is negative, it counts from the end (-1 is last). Load it with @use "sass:list" on Dart Sass 1.23+. Global alias: nth().

Out-of-range indexes error.

Official docs: Sass throws if there is no element at $n. Guard with list.length(), or only call nth after list.index() returns a non-null index.

Official samples.

list.nth(10px 12px 16px, 2) → 12px. list.nth([line1, line2, line3], -1) → line3.

Syntax

SCSS
@use "sass:list";

list.nth(10px 12px 16px, 2);            // 12px
list.nth(10px 12px 16px, 1);            // 10px
list.nth(10px 12px 16px, -1);           // 16px
list.nth([line1, line2, line3], -1);    // line3

// Global alias still works
nth(10px 12px 16px, 2);

Pattern: find a slot with index (or loop 1…length) → read with nth → rewrite with set-nth when needed.

Index Rules

How $n is interpreted:

IndexMeaning
1First item
2, 3, …Later items from the start
-1Last item
-2Second-to-last item
Out of range / nullCompile error — guard first
nth vs index.

index answers “where is this value?” nth answers “what is at this position?” Use them together for safe lookups.

SCSS Examples

Examples target Dart Sass 1.23+ with @use "sass:list". Open View Compiled CSS for verified output (matches official Sass docs samples).

Example 1 — Read the Middle Item (Official Docs)

Index 2 on a three-item spacing list.

SCSS
@use "sass:list";

@debug list.nth(10px 12px 16px, 2); // 12px

.space {
  --mid: #{list.nth(10px 12px 16px, 2)};
  --first: #{list.nth(10px 12px 16px, 1)};
}

How It Works

  1. Indexes start at 1 — first item is 10px.
  2. Index 2 returns 12px.
  3. Interpolation writes the value into CSS.
  4. Matches the official Sass docs sample.

Example 2 — Negative Index From the End (Official Docs)

-1 is the last item; -2 is second-to-last.

SCSS
@use "sass:list";

@debug list.nth([line1, line2, line3], -1); // line3

.bracketed {
  --last: #{list.nth([line1, line2, line3], -1)};
}

.space {
  --second-last: #{list.nth(10px 12px 16px, -2)};
}

How It Works

  1. -1 picks the last bracketed item line3.
  2. -2 on 10px 12px 16px is 12px.
  3. Brackets do not change indexing rules.
  4. Handy for “last fallback” font or spacing tokens.

Example 3 — Split a Border Shorthand

Read width, style, and color by fixed positions.

SCSS
@use "sass:list";

$border: 1px solid red;

.border {
  --width: #{list.nth($border, 1)};
  --style: #{list.nth($border, 2)};
  --color: #{list.nth($border, 3)};
}

How It Works

  1. Space-separated border parts are a three-item list.
  2. Positions 1 / 2 / 3 map to width / style / color.
  3. Fixed indexes work when the shape is guaranteed.
  4. Prefer index() when the order might vary.

Example 4 — Loop 1…length with nth

Safe iteration pattern for spacing scales.

SCSS
@use "sass:list";

$sizes: 8px 16px 24px 32px;

.loop {
  @for $i from 1 through list.length($sizes) {
    --s#{$i}: #{list.nth($sizes, $i)};
  }
}

How It Works

  1. list.length sets the loop upper bound.
  2. @for from 1 matches 1-based indexes.
  3. Each nth reads one spacing step.
  4. Stays in range — no out-of-range errors.

Example 5 — Find with index, Then Read with nth

Null-safe lookup when you know the value but not the slot.

SCSS
@use "sass:list";

$border: 1px solid red;
$i: list.index($border, solid);

.with-index {
  @if $i != null {
    --style: #{list.nth($border, $i)};
  }
}

How It Works

  1. index finds the style slot (or returns null).
  2. @if $i != null blocks a bad nth call.
  3. nth returns solid at that index.
  4. Best default when list order is not fixed.

Edge Cases & Pitfalls

Most nth bugs are zero-based thinking, passing null from index, or reading past list.length.

base

Indexes start at 1

There is no index 0. First item is always 1.

range

Out of range throws

Compare against list.length before hard-coded indexes.

null

Never pass index null

list.nth($list, null) errors. Guard after list.index.

neg

Negative still must exist

-5 on a 3-item list is still out of range.

Sass Support

Prefer the module form with Dart Sass 1.23+. Global nth() remains for older call sites.

APIStatus
list.nth()Dart Sass 1.23+ via @use "sass:list"
Global nth()Available (prefer module form in new code)
Indexes1-based; negatives count from the end
LibSass / Ruby SassNo sass:list module — use global nth() if needed

Compile with the Dart Sass CLI.

Key Takeaways

  • Reads one item by 1-based index.
  • Negative indexes count from the end (-1 = last).
  • Guard out-of-range with length or a non-null index.
  • Prefer list.nth after @use "sass:list".

One line: when you need the value at a position, call list.nth($list, $n).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:list"
2. Remember indexes are 1-based
3. Use -1 for the last item
4. Never pass a null index into nth
5. Loop with @for $i from 1 through list.length($list)

Frequently Asked Questions

list.nth($list, $n) returns the item at index $n. Example: list.nth(10px 12px 16px, 2) is 12px. Indexes start at 1.
Official docs: negative $n counts from the end. list.nth([line1, line2, line3], -1) is line3. -2 is the second-to-last item.
Sass throws an error. Check against list.length, or only call nth after list.index returns a non-null index.
list.index finds a value’s position. list.nth returns the value at a position. They are often used together.
Yes. Global nth($list, $n) still works. New projects should prefer list.nth() after @use "sass:list".
Built-in modules with @use need Dart Sass 1.23+. LibSass and Ruby Sass do not load sass:list the same way—use the global nth() name there if you must.

Next: Sass list.set-nth()

Replace the value at a 1-based (or negative) index.

list.set-nth() →

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