Sass list.index() Function

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

What You’ll Learn

list.index() belongs to the built-in sass:list module. It finds where a value sits in a list (or returns null). This page covers 1-based indexes, missing values, duplicates, pairing with list.nth, Dart Sass support, and five compiled examples.

01

Concept

Find a position

02

Module

@use "sass:list"

03

Indexes

1-based

04

Missing

Returns null

05

Pair with

list.nth

06

Practice

5 examples

What Is list.index()?

Imagine a space-separated CSS value like 1px solid red. You may need to ask: “Which slot holds solid?” list.index answers with a number at compile time.

  • list.index(1px solid red, 1px)1
  • list.index(1px solid red, solid)2
  • list.index(1px solid red, dashed)null
💡
Beginner tip

Sass list indexes start at 1 (not 0 like many programming languages). The browser never sees list.index—only the numbers or values you emit.

📝 Syntax

Load the list module, then call the function:

styles.scss
@use "sass:list";

list.index($list, $value)

Parameters

ParameterTypeRequiredDescription
$listList (or map / single value)YesThe list to search. Maps and single values also count as lists in Sass.
$valueAnyYesThe item to look for (compared as a Sass value).

Return value

TypeSituationResult
Number$value is found1-based index of the first match
null$value is missingNo position

📦 Loading sass:list

styles.scss
// Recommended — namespaced
@use "sass:list";
$i: list.index(1px solid red, solid);

// Optional — bring members into scope
@use "sass:list" as *;
$i: index(1px solid red, solid);

// Optional — custom namespace
@use "sass:list" as l;
$i: l.index(1px solid red, solid);
⚠️
Put @use first

Keep @use "sass:list"; near the top of the file, before most other rules.

🔍 Handling null

Missing values return null. Always guard before you pass the result into list.nth, which errors on a bad index.

styles.scss
@use "sass:list";

$gaps: 0.5rem 1rem 1.5rem;
$i: list.index($gaps, 1rem);

$space: if($i != null, list.nth($gaps, $i), 0);

📋 list.index vs list.nth

list.indexlist.nth
QuestionWhere is this value?What is at this index?
InputList + valueList + number
OutputNumber or nullValue (or error if out of range)
Typical pairFind $iThen list.nth($list, $i)

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS only contains the finished numbers or property values you choose to emit.

Implementation@use "sass:list"Global index()
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";
Find solidlist.index(1px solid red, solid)2
Missing valuelist.index(1px solid red, dashed)null
First duplicatelist.index(10px 20px 10px, 10px)1
Safe use with nthif($i != null, list.nth($list, $i), $fallback)
Legacy globalindex($list, $value)

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 find and miss.

Example 1 — Find Positions in a Border List (Official Docs)

Space-separated border values are a natural Sass list.

styles.scss
@use "sass:list";

@debug list.index(1px solid red, 1px);    // 1
@debug list.index(1px solid red, solid);  // 2

.box {
  --width-at: #{list.index(1px solid red, 1px)};
  --style-at: #{list.index(1px solid red, solid)};
}

How It Works

Sass treats 1px solid red as three items. Indexes start at 1, so solid is in slot 2.

Example 2 — Missing Value Returns null (Official Docs)

Inspect a miss safely with meta.inspect.

styles.scss
@use "sass:list";
@use "sass:meta";

@debug list.index(1px solid red, dashed); // null

$gaps: 0.5rem 1rem 1.5rem;
$hit: list.index($gaps, 2rem);
$mid: list.index($gaps, 1rem);

.demo {
  --hit: #{meta.inspect($hit)};
  --mid: #{$mid};
  margin-top: if($mid != null, list.nth($gaps, $mid), 0);
}

How It Works

2rem is not in $gaps, so $hit is null. 1rem is found at index 2 and used with list.nth.

📈 Practical Patterns

Borders, font stacks, and duplicate tokens.

Example 3 — Pull Border Style by Index

Find solid, then read that slot with list.nth.

styles.scss
@use "sass:list";

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

.box {
  --solid-at: #{$i};
  border-style: if($i != null, list.nth($border, $i), none);
}

How It Works

Index finds the style keyword; nth returns it for border-style. The if guard keeps missing keywords safe.

Example 4 — Locate a Font in a Stack

Comma-separated font lists work the same way.

styles.scss
@use "sass:list";

$fonts: Helvetica, Arial, sans-serif;
$pos: list.index($fonts, Arial);

.card {
  --arial: #{$pos};
}

How It Works

Arial is the second family in the stack, so the index is 2.

Example 5 — First Match Wins on Duplicates

Repeated values still return only the earliest index.

styles.scss
@use "sass:list";

$dup: 10px 20px 10px;

.dup {
  --first: #{list.index($dup, 10px)};
}

How It Works

Official docs: if a value appears multiple times, you get the index of its first appearance—here slot 1, not 3.

🚀 Real-World Use Cases

  • Border / background lists — find width, style, or color slots.
  • Design scales — check whether a spacing token exists before using it.
  • Font stacks — locate a family before swapping with list.set-nth.
  • Feature flags in lists — test membership via != null.
  • Mixin guards — skip logic when a required keyword is absent.

🧠 How Compilation Works

1

Write SCSS

Call list.index($list, $value) after @use "sass:list".

Source
2

Scan the list

Dart Sass walks items from the start looking for the first match.

Compile
3

Return index or null

A hit yields a 1-based number; a miss yields null.

Result
4

CSS sees numbers

Custom properties or nth-based values ship without any Sass call.

⚠️ Common Pitfalls

  • 0-based thinking — the first item is 1, not 0.
  • Using null with nth — always check $i != null first.
  • Assuming last duplicate — only the first match is returned.
  • Forgetting @uselist.index needs sass:list.
  • Type mismatches1 and 1px are different Sass values.

💡 Best Practices

✅ Do

  • Use @use "sass:list" and list.index() in new SCSS
  • Guard with if($i != null, …) before list.nth
  • Treat membership tests as list.index(...) != null
  • Remember indexes are 1-based like list.nth
  • Prefer Dart Sass for built-in modules

❌ Don’t

  • Expect the browser to evaluate list.index
  • Pass a null index straight into list.nth
  • Assume a later duplicate will be found
  • Rely on LibSass for @use "sass:list"
  • Compare values with mismatched units or types

Key Takeaways

Knowledge Unlocked

Five things to remember about list.index()

1-based search—null when missing, first hit when duplicated.

5
Core concepts
📦 02

Module

sass:list

@use
🔢 03

Indexes

start at 1

Rule
04

Miss

null

Safe
05

Prefer

Dart Sass 1.23+

Tooling

❓ Frequently Asked Questions

list.index($list, $value) returns the 1-based position of $value in $list. Example: list.index(1px solid red, solid) is 2. If the value is missing, it returns null.
1-based. The first item is index 1, not 0—the same convention as list.nth().
Official docs: the function returns null. Use != null checks before calling list.nth with that index.
list.index returns the index of the first appearance only.
Yes. Global index($list, $value) still works. New projects should prefer list.index() 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 index() name there if you must.
Did you know?

Because individual values count as one-item lists, list.index(10px, 10px) is 1. That same rule makes maps searchable as lists of key/value pairs when you work with advanced list helpers.

Conclusion

list.index() finds the 1-based position of a value in a Sass list, or returns null when it is missing. Load it through sass:list, guard nulls before list.nth, and remember that duplicates resolve to the first match.

Continue with list.is-bracketed() or explore list.append().

Next: list.is-bracketed()

You learned list.index()—next, detect square-bracket lists with list.is-bracketed().

list.is-bracketed() →

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