list.index($list, $value) returns the 1-based index of $value in $list, or null if it is missing. Load it with @use "sass:list" on Dart Sass 1.23+. Global alias: index().
Official docs: if $value appears more than once, you get the index of its first appearance. Pair with list.nth() when you need the value at that position.
When a value appears twice, only the earliest index is returned.
SCSS
@use "sass:list";
@debug list.index(red green red blue, red); // 1
@debug list.index(red green red blue, blue); // 4
.dupes {
--first-red: #{list.index(red green red blue, red)};
--blue: #{list.index(red green red blue, blue)};
}
Compiled CSS
.dupes {
--first-red: 1;
--blue: 4;
}
How It Works
red appears twice (positions 1 and 3).
First appearance wins → index 1.
blue is unique at index 4.
No built-in “last index” helper — scan yourself if you need it.
1-based lookup of a value in a list (or map-as-list).
Missing values return null — guard before nth.
Duplicates resolve to the first appearance only.
Prefer list.index after @use "sass:list".
One line: when you need a value’s position in a list, call list.index($list, $value).
Practice
Compile Notes
Practical checklist:
checklist.txt
1. @use "sass:list"
2. Remember indexes are 1-based
3. Handle null when the value is missing
4. Expect the first match for duplicates
5. Pair with list.nth / list.set-nth after a successful find
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.