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
Concept
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.
Foundation
📝 Syntax
Load the list module, then call the function:
styles.scss
@use "sass:list";
list.index($list, $value)
Parameters
Parameter
Type
Required
Description
$list
List (or map / single value)
Yes
The list to search. Maps and single values also count as lists in Sass.
Official docs: if a value appears multiple times, you get the index of its first appearance—here slot 1, not 3.
Applications
🚀 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.
Watch Out
⚠️ 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 @use — list.index needs sass:list.
Type mismatches — 1 and 1px are different Sass values.
Pro Tips
💡 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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about list.index()
1-based search—null when missing, first hit when duplicated.
5
Core concepts
📝01
Call
list.index($list, $value)
API
📦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.
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.