Sass list.length()

Beginner
10 min read
Updated: Nov 2026
5 examples
Dart Sass 1.23+

Definition and Usage

list.length($list) returns how many items are in $list. Load it with @use "sass:list" on Dart Sass 1.23+. Global alias: length().

Official docs: a single value counts as a one-item list (length 1), and a map returns the number of key/value pairs. Pair with list.nth() when looping by index.

Official samples.

list.length(10px) → 1. list.length(10px 20px 30px) → 3. list.length((width: 10px, height: 20px)) → 2.

Nested lists count as one item.

list.append(10px 20px, 30px 40px) has length 3, not 4. Use list.join() when you want a flat count.

Syntax

SCSS
@use "sass:list";

list.length(10px);                          // 1
list.length(10px 20px 30px);                // 3
list.length((width: 10px, height: 20px));   // 2
list.length(());                            // 0
list.length([]);                            // 0

// Global alias still works
length(10px 20px 30px);

Pattern: read length → guard or loop 1…length with nth.

What Counts

How different shapes are counted:

InputLength
Single value 10px1
Space / comma list of n itemsn
Map with n pairsn (pairs, not keys alone)
() or []0
Nested item from appendCounts as one top-level item
Brackets do not change length.

list.length([a, b, c]) is still 3. Bracketedness is a separate flag — see list.is-bracketed().

SCSS Examples

Examples target Dart Sass 1.23+ with @use "sass:list". Open View Compiled CSS for verified output (matches official Sass docs samples).

Example 1 — A Single Value Has Length 1 (Official Docs)

Individual values count as one-item lists.

SCSS
@use "sass:list";

@debug list.length(10px); // 1

.single {
  --len: #{list.length(10px)};
}

How It Works

  1. 10px is treated as a one-item list.
  2. Length is 1, not an error.
  3. Same idea powers join(10px, 20px).
  4. Matches the official Sass docs sample.

Example 2 — Count a Space-Separated List (Official Docs)

Three spacing tokens → length 3.

SCSS
@use "sass:list";

@debug list.length(10px 20px 30px); // 3

.space {
  --len: #{list.length(10px 20px 30px)};
}

How It Works

  1. Space-separated values are separate items.
  2. Count is 3.
  3. Useful before a 1…n loop with nth.
  4. Matches the official Sass docs sample.

Example 3 — Maps Return Pair Counts (Official Docs)

Official fun fact: maps count as lists of key/value pairs.

SCSS
@use "sass:list";

@debug list.length((width: 10px, height: 20px)); // 2

.map-like {
  --len: #{list.length((width: 10px, height: 20px))};
}

How It Works

  1. Two map pairs → length 2.
  2. Not a count of nested values inside each pair.
  3. Prefer map helpers for key lookups in real themes.
  4. Matches the official Sass docs sample.

Example 4 — Empty Lists, Commas, and Brackets

Length ignores whether brackets are present.

SCSS
@use "sass:list";

.shapes {
  --empty-parens: #{list.length(())};
  --empty-brackets: #{list.length([])};
  --comma: #{list.length((blue, red, green))};
  --bracketed: #{list.length([1fr 2fr 1fr])};
}

How It Works

  1. Empty lists — () and [] — both have length 0.
  2. Comma list of three colors → 3.
  3. Bracketed tracks still count three items.
  4. Use is-bracketed if you need the bracket flag separately.

Example 5 — Length Reveals append vs join

Same inputs, different structure — length makes it obvious.

SCSS
@use "sass:list";

$sizes: 8px 16px 24px 32px;

.compare {
  --append-len: #{list.length(list.append(10px 20px, 30px 40px))};
  --join-len: #{list.length(list.join(10px 20px, 30px 40px))};
}

.guard {
  @if list.length($sizes) >= 3 {
    --ok: true;
    --count: #{list.length($sizes)};
  } @else {
    --ok: false;
  }
}

How It Works

  1. Append nests → length 3.
  2. Join flattens → length 4.
  3. Guard uses length before trusting a spacing scale.
  4. Count first, then loop or rewrite safely.

Edge Cases & Pitfalls

Most length bugs are expecting nested items to count separately, or confusing brackets with item count.

nest

Nested lists count as one item

append nesting keeps a shorter length than a flattened join.

single

Singles have length 1

Do not special-case lone values unless you need to — they are valid lists.

map

Maps count pairs

list.length($map) is pair count, not a deep walk of nested maps.

empty

Empty lists are 0

Guard before nth when length might be zero.

Sass Support

Prefer the module form with Dart Sass 1.23+. Global length() remains for older call sites.

APIStatus
list.length()Dart Sass 1.23+ via @use "sass:list"
Global length()Available (prefer module form in new code)
Return typeNumber (≥ 0)
LibSass / Ruby SassNo sass:list module — use global length() if needed

Compile with the Dart Sass CLI.

Key Takeaways

  • Counts top-level items in a list (or pairs in a map).
  • Singles are length 1; empty lists are 0.
  • Nested append items still count as one slot.
  • Prefer list.length after @use "sass:list".

One line: when you need how many items a list has, call list.length($list).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:list"
2. Remember singles have length 1
3. Maps return pair counts
4. Use length to spot append nesting vs join flattening
5. Guard empty lists before nth / set-nth

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, and list.length([]) is also 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.

Next: Sass list.separator()

Read whether a list uses space, comma, or slash.

list.separator() →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful