Sass list.nth() Function

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:list · Dart Sass 1.23+

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

02

Module

@use "sass:list"

03

Indexes

1-based

04

Negative

From the end

05

Pair with

length / index

06

Practice

5 examples

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)12px
  • list.nth([line1, line2, line3], -1)line3
  • list.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.

📝 Syntax

Load the list module, then call the function:

styles.scss
@use "sass:list";

list.nth($list, $n)

Parameters

ParameterTypeRequiredDescription
$listList (or map / single value)YesThe list to read from.
$nNumber (integer)Yes1-based index. Negative values count from the end. Must point at an existing item or Sass errors.

Return value

TypeResult
AnyThe element at index $n
ErrorIf there is no element at that index

📦 Loading sass:list

styles.scss
// 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.

🔍 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.

styles.scss
@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)

📋 list.nth vs list.index

list.nthlist.index
QuestionWhat is at this index?Where is this value?
InputList + numberList + value
OutputValue (or error)Number or null
Typical pairlist.nth($list, $i)Find $i first, then guard null

🛠 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 — preferredYes
LibSassNo built-in modulesYes (legacy)
Ruby SassNo built-in modulesYes (legacy)

⚡ Quick Reference

GoalCode
Import list@use "sass:list";
Second itemlist.nth(10px 12px 16px, 2)12px
Last itemlist.nth($list, -1)
Safe loop@for $i from 1 through list.length($list)
Find then readif($i != null, list.nth($list, $i), $fallback)
Legacy globalnth($list, $n)

Examples Gallery

Each example uses @use "sass:list". Open View Compiled CSS for verified Dart Sass output (official samples where noted).

📚 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.

styles.scss
@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)};
}

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.

styles.scss
@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);
}

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.

styles.scss
@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)};
}

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.

styles.scss
@use "sass:list";

$fonts: Helvetica, Arial, sans-serif;

.card {
  --primary: #{list.nth($fonts, 1)};
  --fallback: #{list.nth($fonts, -1)};
  font-family: $fonts;
}

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.

styles.scss
@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);
}

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.

🚀 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.

⚠️ 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 @uselist.nth needs sass:list.
  • Confusing with set-nthnth reads; set-nth returns a modified copy.

💡 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"

Key Takeaways

Knowledge Unlocked

Five things to remember about list.nth()

1-based read by index—negatives from the end, errors if missing.

5
Core concepts
📦 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

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.
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.

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

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.

5 people found this page helpful