Home Sass List list.nth() Sass list.nth() Definition
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
Syntax @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.
Code
SCSS Examples Examples target Dart Sass 1.23+ with @use "sass:list". Open View Compiled CSS for verified output (matches official Sass docs samples).
1. Mid item 2. Negative 3. Border parts 4. Loop 5. With index Example 1 — Read the Middle Item (Official Docs) Index 2 on a three-item spacing list.
@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)};
} .space {
--mid: 12px;
--first: 10px;
} How It Works Indexes start at 1 — first item is 10px.Index 2 returns 12px.Interpolation writes the value into CSS.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.
@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)};
} .bracketed {
--last: line3;
}
.space {
--second-last: 12px;
} How It Works -1 picks the last bracketed item line3.-2 on 10px 12px 16px is 12px.Brackets do not change indexing rules.Handy for “last fallback” font or spacing tokens.Example 3 — Split a Border Shorthand Read width, style, and color by fixed positions.
@use "sass:list";
$border: 1px solid red;
.border {
--width: #{list.nth($border, 1)};
--style: #{list.nth($border, 2)};
--color: #{list.nth($border, 3)};
} .border {
--width: 1px;
--style: solid;
--color: red;
} How It Works Space-separated border parts are a three-item list.Positions 1 / 2 / 3 map to width / style / color.Fixed indexes work when the shape is guaranteed.Prefer index() when the order might vary.Example 4 — Loop 1…length with nth Safe iteration pattern for spacing scales.
@use "sass:list";
$sizes: 8px 16px 24px 32px;
.loop {
@for $i from 1 through list.length($sizes) {
--s#{$i}: #{list.nth($sizes, $i)};
}
} .loop {
--s1: 8px;
--s2: 16px;
--s3: 24px;
--s4: 32px;
} How It Works list.length sets the loop upper bound.@for from 1 matches 1-based indexes.Each nth reads one spacing step.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.
@use "sass:list";
$border: 1px solid red;
$i: list.index($border, solid);
.with-index {
@if $i != null {
--style: #{list.nth($border, $i)};
}
} .with-index {
--style: solid;
} How It Works index finds the style slot (or returns null).@if $i != null blocks a bad nth call.nth returns solid at that index.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.
Toolchain
Sass Support Prefer the module form with Dart Sass 1.23+. Global nth() remains for older call sites.
API Status list.nth()Dart Sass 1.23+ via @use "sass:list" Global nth() Available (prefer module form in new code) Indexes 1-based; negatives count from the end LibSass / Ruby Sass No sass:list module — use global nth() if needed
Compile with the Dart Sass CLI .
Remember
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).
Practice
Compile Notes Practical checklist:
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 What does Sass list.nth() do? list.nth($list, $n) returns the item at index $n. Example: list.nth(10px 12px 16px, 2) is 12px. Indexes start at 1.
What do negative indexes mean? Official docs: negative $n counts from the end. list.nth([line1, line2, line3], -1) is line3. -2 is the second-to-last item.
What happens if the index is out of range? Sass throws an error. Check against list.length, or only call nth after list.index returns a non-null index.
How is list.nth different from list.index? list.index finds a value’s position. list.nth returns the value at a position. They are often used together.
Can I still call nth() without the list. prefix? Yes. Global nth($list, $n) still works. New projects should prefer list.nth() after @use "sass:list".
Which compilers support 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() → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion