list.length() belongs to the built-in sass:list module. It counts how many items a list holds. This page covers single values, empty lists, maps as lists of pairs, looping with @for and list.nth, Dart Sass support, and five compiled examples.
01
Concept
Count items
02
Module
@use "sass:list"
03
Singles
Length 1
04
Maps
Count pairs
05
Pair with
@for + nth
06
Practice
5 examples
Concept
What Is list.length()?
Before you loop a spacing scale or check whether a font stack has enough fallbacks, you need a count. list.length returns that number at compile time.
list.length(10px) → 1
list.length(10px 20px 30px) → 3
list.length((width: 10px, height: 20px)) → 2
💡
Beginner tip
In Sass, every value is also a list. That is why a lone 10px has length 1—not “not a list.” Maps count as lists of key/value pairs.
Foundation
📝 Syntax
Load the list module, then call the function:
styles.scss
@use "sass:list";
list.length($list)
Parameters
Parameter
Type
Required
Description
$list
List (or map / single value)
Yes
The list to measure. Maps and single values also count as lists in Sass.
Return value
Type
Result
Number
How many items are in $list (or how many pairs, for a map)
Modules
📦 Loading sass:list
styles.scss
// Recommended — namespaced
@use "sass:list";
$n: list.length(10px 20px 30px);
// Optional — bring members into scope
@use "sass:list" as *;
$n: length(10px 20px 30px);
// Optional — custom namespace
@use "sass:list" as l;
$n: l.length(10px 20px 30px);
⚠️
Put @use first
Keep @use "sass:list"; near the top of the file, before most other rules.
Details
🔍 Length of Maps
Official docs note that every map counts as a list of two-element pairs. So list.length on a map returns the number of entries—handy before you loop keys or validate a theme object.
Comma separators do not change counting—three families means length 3, while the stack still compiles as normal CSS.
Applications
🚀 Real-World Use Cases
Utility generators — drive @for loops over spacing or size scales.
Theme validation — assert a map has the expected number of tokens.
Mixin guards — skip logic when a list is empty (length == 0).
Font / shadow stacks — document or debug how many items you emit.
Safe indexing — compare an index against length before calling list.nth.
🧠 How Compilation Works
1
Write SCSS
Call list.length($list) after @use "sass:list".
Source
2
Count items
Dart Sass tallies list items (or map pairs) at compile time.
Compile
3
Return a number
Use it in custom properties, @if guards, or @for bounds.
Result
4
✓
CSS sees numbers
No Sass call remains—only the finished values you emitted.
Watch Out
⚠️ Common Pitfalls
0-based loops — Sass list indexes start at 1, so use from 1 through length.
Nested lists — a nested sublist counts as one item unless you flatten first.
Expecting CSS length() — this is Sass compile-time, not the CSS length() idea.
Forgetting @use — list.length needs sass:list.
Confusing maps with lists of values — map length is pair count, not flattened values.
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:list" and list.length() in new SCSS
Drive @for with 1 through list.length($list)
Treat singles as length 1 when writing helpers
Use length to guard empty lists before indexing
Prefer Dart Sass for built-in modules
❌ Don’t
Expect the browser to evaluate list.length
Start list loops at 0
Assume brackets change the count
Rely on LibSass for @use "sass:list"
Call list.nth past list.length without a check
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about list.length()
Count items (or map pairs)—then loop safely with 1-based indexes.
5
Core concepts
📝01
Call
list.length($list)
API
📦02
Module
sass:list
@use
🔢03
Singles
length 1
Rule
❓04
Maps
count pairs
Safe
✓05
Prefer
Dart Sass 1.23+
Tooling
❓ Frequently Asked Questions
list.length($list) returns how many items are in $list. Example: list.length(10px 20px 30px) is 3. A single value like 10px has length 1.
Yes. Official docs: it can return the number of pairs in a map. list.length((width: 10px, height: 20px)) is 2.
0. For example list.length(()) is 0.
No. Bracketedness is separate from length. list.length([a, b, c]) is still 3.
Yes. Global length($list) still works. New projects should prefer list.length() 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 length() name there if you must.
Did you know?
Because maps are lists of pairs, advanced list helpers like list.nth can also work on maps. Still, for map-specific work prefer sass:map helpers such as map.keys and map.values.
list.length() counts items in a Sass list (or pairs in a map). Load it through sass:list, remember that singles have length 1, and pair it with @for plus list.nth for safe loops.