Home Sass Functions list list.nth() Sass list.nth() Function Overview
What You’ll Learn list.nth() belongs to the built-in sass:list module. It returns the value at a given index. This page covers 1-based indexes, negative indexes from the end, out-of-range errors, pairing with list.length / list.index, Dart Sass support, and five compiled examples.
01
Concept Get item by index
05
Pair with length / index
Concept
What Is list.nth()? Once you know where something sits in a list (or you already know the slot number), list.nth pulls that value out at compile time—perfect for border parts, spacing scales, and font stacks.
list.nth(10px 12px 16px, 2) → 12pxlist.nth([line1, line2, line3], -1) → line3list.nth(10px 12px 16px, 1) → 10px (first item)💡
Beginner tip Sass list indexes start at 1 , not 0. Negative numbers count backward: -1 is the last item. Asking for a missing index stops the compile with an error.
Foundation
📝 Syntax Load the list module, then call the function:
@use "sass:list";
list.nth($list, $n) Parameters Parameter Type Required Description $listList (or map / single value) Yes The list to read from. $nNumber (integer) Yes 1-based index. Negative values count from the end. Must point at an existing item or Sass errors.
Return value Type Result Any The element at index $n Error If there is no element at that index
Modules
📦 Loading sass:list // Recommended — namespaced
@use "sass:list";
$mid: list.nth(10px 12px 16px, 2);
// Optional — bring members into scope
@use "sass:list" as *;
$mid: nth(10px 12px 16px, 2);
// Optional — custom namespace
@use "sass:list" as l;
$mid: l.nth(10px 12px 16px, 2); ⚠️
Put @use first Keep @use "sass:list"; near the top of the file, before most other rules.
Details
🔍 Negative Indexes Counting from the end is handy when you want the last font fallback or the largest spacing token without hard-coding list.length.
@use "sass:list";
$gaps: 0.25rem 0.5rem 1rem 1.5rem 2rem;
list.nth($gaps, -1); // 2rem (last)
list.nth($gaps, -2); // 1.5rem (second-to-last) Compare
📋 list.nth vs list.index list.nthlist.indexQuestion What is at this index? Where is this value? Input List + number List + value Output Value (or error) Number or null Typical pair list.nth($list, $i)Find $i first, then guard null
Support
🛠 Compatibility This is about Sass compilers , not browsers. Compiled CSS only contains the finished values you choose to emit.
Implementation @use "sass:list"Global nth() Dart Sass (1.23+) Yes — preferred Yes LibSass No built-in modules Yes (legacy) Ruby Sass No built-in modules Yes (legacy)
Cheat Sheet
⚡ Quick Reference Goal Code Import list @use "sass:list";Second item list.nth(10px 12px 16px, 2) → 12pxLast item list.nth($list, -1)Safe loop @for $i from 1 through list.length($list)Find then read if($i != null, list.nth($list, $i), $fallback)Legacy global nth($list, $n)
Hands-On
Examples Gallery Each example uses @use "sass:list". Open View Compiled CSS for verified Dart Sass output (official samples where noted).
basic border negative fonts with-index 📚 Getting Started Official docs samples for positive and negative indexes.
Example 1 — Positive and Negative Indexes (Official Docs) Read the middle of a space list and the last item of a bracketed list.
@use "sass:list";
@debug list.nth(10px 12px 16px, 2); // 12px
@debug list.nth([line1, line2, line3], -1); // line3
.demo {
--mid: #{list.nth(10px 12px 16px, 2)};
--last: #{list.nth([line1, line2, line3], -1)};
} .demo {
--mid: 12px;
--last: line3;
} How It Works Index 2 is the second item (12px). Index -1 is the last item in the bracketed list (line3).
Example 2 — Split a Border Shorthand Pull width, style, and color into separate longhand properties.
@use "sass:list";
$border: 1px solid #336699;
.box {
border-width: list.nth($border, 1);
border-style: list.nth($border, 2);
border-color: list.nth($border, 3);
} .box {
border-width: 1px;
border-style: solid;
border-color: #336699;
} How It Works A space-separated border value is three list items. Indexes 1–3 map cleanly to width, style, and color.
📈 Practical Patterns Scales, fonts, and safe index→nth pipelines.
Example 3 — First, Last, and Second-to-Last from a Scale Use positive and negative indexes on the same spacing list.
@use "sass:list";
$gaps: 0.25rem 0.5rem 1rem 1.5rem 2rem;
.scale {
--first: #{list.nth($gaps, 1)};
--last: #{list.nth($gaps, -1)};
--second-last: #{list.nth($gaps, -2)};
} .scale {
--first: 0.25rem;
--last: 2rem;
--second-last: 1.5rem;
} How It Works 1 is the start of the scale; -1 and -2 walk backward from the end without computing length first.
Example 4 — Primary Font and Final Fallback Comma-separated stacks work the same way.
@use "sass:list";
$fonts: Helvetica, Arial, sans-serif;
.card {
--primary: #{list.nth($fonts, 1)};
--fallback: #{list.nth($fonts, -1)};
font-family: $fonts;
} .card {
--primary: Helvetica;
--fallback: sans-serif;
font-family: Helvetica, Arial, sans-serif;
} How It Works The first family is index 1; the generic fallback is the last item at -1. The full stack still compiles as font-family.
Example 5 — Find with list.index, Then Read with list.nth Guard missing values so you never pass a null index into nth.
@use "sass:list";
$gaps: 0.25rem 0.5rem 1rem 1.5rem 2rem;
$i: list.index($gaps, 1rem);
.hit {
margin-top: if($i != null, list.nth($gaps, $i), 0);
} .hit {
margin-top: 1rem;
} How It Works list.index() returns 3 for 1rem. The if guard keeps a missing token from crashing the compile when you call list.nth.
Applications
🚀 Real-World Use Cases Shorthand splitting — border, margin, or background token lists.Design scales — pick first/last spacing or size tokens.Font stacks — expose primary family vs final fallback.Utility generators — @for with list.nth($list, $i).Safe lookups — combine list.index + null guard + list.nth.🧠 How Compilation Works 1
Write SCSS Call list.nth($list, $n) after @use "sass:list".
Source
2
Resolve the index Dart Sass maps positive indexes from the start and negatives from the end.
Compile
3
Return the value A valid slot yields the item; an invalid slot stops the compile.
Result
4
✓ CSS sees the value Properties receive the finished token—no Sass call remains.
Watch Out
⚠️ Common Pitfalls 0-based thinking — the first item is 1, not 0.Out-of-range indexes — Sass errors; check list.length first.Null from index — never pass null straight into list.nth.Forgetting @use — list.nth needs sass:list.Confusing with set-nth — nth reads; set-nth returns a modified copy.Pro Tips
💡 Best Practices ✅ Do Use @use "sass:list" and list.nth() in new SCSS Loop with 1 through list.length($list) Use -1 when you want the last item Guard list.index results before calling list.nth Prefer Dart Sass for built-in modules ❌ Don’t Expect the browser to evaluate list.nth Start list indexes at 0 Call nth with a guessed index past the end Pass a null index from a failed search Rely on LibSass for @use "sass:list" Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about list.nth() 1-based read by index—negatives from the end, errors if missing.
📝 01
Call list.nth($list, $n)
API 📦 02
Module sass:list
@use 🔢 03
Indexes start at 1
Rule ❓ 04
Negative −1 is last
Safe ✓ 05
Prefer Dart Sass 1.23+
Tooling ❓ 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.
Did you know?
To replace an item instead of reading it, use list.set-nth($list, $n, $value). It returns a new list—list.nth never mutates the original.
Wrap Up
Conclusion list.nth() returns the value at a 1-based index, with negatives counting from the end. Load it through sass:list, stay inside list.length, and pair it with a null-safe list.index when searching.
Continue with list.set-nth() or explore list.index() .
Next: list.set-nth() You learned list.nth()—next, replace items by index with list.set-nth().
list.set-nth() → 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.
5 people found this page helpful
Helpful Share Copy link Suggestion