Sass map.keys() Function

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:map · returns a list

What You’ll Learn

map.keys() belongs to the built-in sass:map module. It returns every key in a map as a comma-separated list. This page covers order, @each loops, nested maps, vs map.values, the legacy map-keys name, and five compiled examples.

01

Concept

List all keys

02

Module

@use "sass:map"

03

Returns

Comma-separated list

04

vs values

Names vs numbers

05

Loops

@each + get

06

Practice

5 examples

What Is map.keys()?

Maps store named pairs like "medium": 500. Sometimes you need the names—to loop, build class names, or count entries. Official docs: map.keys returns a comma-separated list of all keys in $map.

  • Order matches the map’s key order
  • Only top-level keys of the map you pass (not deep nested keys automatically)
  • Result is a list—use list.length, list.nth, or @each
💡
Beginner tip

Think of a table of contents: map.keys lists the chapter titles. map.values lists the page numbers. map.get opens one chapter.

📝 Syntax

Load the map module, then call the function:

styles.scss
@use "sass:map";

map.keys($map)

// Legacy global name (still works)
map-keys($map)

Parameters

ParameterTypeRequiredDescription
$mapMapYesMap whose keys you want. Not modified.

Return value

TypeResult
ListComma-separated list of all keys, in map order (empty list for an empty map)

📦 Loading sass:map

styles.scss
// Recommended — namespaced
@use "sass:map";
$names: map.keys($theme);

// Optional — bring members into scope
@use "sass:map" as *;
$names: keys($theme);

// Optional — custom namespace
@use "sass:map" as m;
$names: m.keys($theme);

// Legacy global
$names: map-keys($theme);
⚠️
Module vs legacy

Prefer map.keys with @use "sass:map". Keep map-keys for older snippets and LibSass-era code.

📋 map.keys vs map.values

Both return comma-separated lists in the same order. Pick the side of each pair you need.

map.keysmap.values
ReturnsKey namesStored values
Best forSelectors, @each names, counting entriesBulk math on numbers/colors
Typical follow-upmap.get($map, $key)Use list helpers directly
Legacy namemap-keysmap-values

🔍 Top-Level Only (Nested Maps)

map.keys($theme) does not flatten nested keys. If $theme has a "colors" map inside, you only see "colors" at the top level. To list color token names, pass the nested map:

styles.scss
@use "sass:map";

map.keys($theme); // "colors", "radius"
map.keys(map.get($theme, "colors")); // "primary", "accent"

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS only contains the values you emit after looping or reading keys.

Implementationmap.keys() / map-keysNotes
Dart SassYesPrefer @use "sass:map" (module API since 1.23+)
LibSassUse global map-keysNo modern sass:map module
Ruby SassLegacy map-keysPrefer Dart Sass

⚡ Quick Reference

GoalCode
Import map@use "sass:map";
All keysmap.keys($map)
Count keyslist.length(map.keys($map))
Loop keys@each $k in map.keys($map) { ... }
Nested map keysmap.keys(map.get($map, "colors"))
Legacy namemap-keys($map)

Examples Gallery

Each example uses @use "sass:map". Open View Compiled CSS for verified Dart Sass output (official sample where noted).

📚 Getting Started

Official docs sample and list helpers.

Example 1 — List All Keys (Official Docs)

Pull every weight name from a font-weight map.

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

$font-weights: (
  "regular": 400,
  "medium": 500,
  "bold": 700
);

@debug map.keys($font-weights); // "regular", "medium", "bold"

.weights {
  --all: #{meta.inspect(map.keys($font-weights))};
}

How It Works

Keys appear in map order as a comma-separated list. Values (400, 500, 700) are not included—use map.values for those.

Example 2 — Length, First Key, Empty Map

Treat the result like any other list with sass:list.

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

$font-weights: (
  "regular": 400,
  "medium": 500,
  "bold": 700
);
$empty: ();

.keys {
  --all: #{meta.inspect(map.keys($font-weights))};
  --len: #{list.length(map.keys($font-weights))};
  --first: #{list.nth(map.keys($font-weights), 1)};
}

.empty {
  --keys: #{meta.inspect(map.keys($empty))};
  --len: #{list.length(map.keys($empty))};
}

How It Works

Three keys → length 3. An empty map yields an empty list () with length 0.

📈 Practical Patterns

Nested maps, @each generators, and keys vs values.

Example 3 — Keys of a Nested Map

List top-level theme keys, then dig into colors for token names.

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

$theme: (
  "colors": (
    "primary": #336699,
    "accent": #e67e22
  ),
  "radius": 8px
);

.theme {
  --top: #{meta.inspect(map.keys($theme))};
  --color-keys: #{meta.inspect(map.keys(map.get($theme, "colors")))};
}

How It Works

Top-level keys are only colors and radius. Passing the nested colors map lists primary and accent.

Example 4 — Generate Classes with @each

Loop keys to build utility classes from a spacing map.

styles.scss
@use "sass:map";

$spacing: (
  "sm": 0.5rem,
  "md": 1rem,
  "lg": 1.5rem
);

@each $name in map.keys($spacing) {
  .space-#{$name} {
    padding: map.get($spacing, $name);
  }
}

How It Works

Each key becomes part of a class name; map.get supplies the padding value. (You can also write @each $name, $size in $spacing—keys + get is clearer when you already think in map helpers.)

Example 5 — Keys Beside Values

Side-by-side lists from the same map.

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

$font-weights: (
  "regular": 400,
  "medium": 500,
  "bold": 700
);

.compare {
  --keys: #{meta.inspect(map.keys($font-weights))};
  --values: #{meta.inspect(map.values($font-weights))};
}

How It Works

Same order, different sides of each pair. Index 1 in both lists refers to the same entry (regular / 400).

🚀 Real-World Use Cases

  • Utility generators — build .space-sm-style classes from a token map.
  • Theme introspection — list which top-level sections a config map defines.
  • Validation — compare map.keys against an allowed-keys list.
  • Docs / debug — print key names with meta.inspect while building systems.
  • Nested tokensmap.keys(map.get(...)) to list color or type scale names.

🧠 How Compilation Works

1

Write SCSS

Call map.keys($map) after @use "sass:map".

Source
2

Collect keys

Dart Sass builds a comma-separated list in map order.

Compile
3

Loop or inspect

Use @each, list.length, or pair with map.get.

Result
4

CSS sees output

Only the rules or custom properties you emit remain.

⚠️ Common Pitfalls

  • Expecting nested keyskeys is top-level only; dig with map.get first.
  • Confusing keys with values — names vs stored data; use the matching helper.
  • Forgetting it is a list — use list module helpers, not map helpers, on the result.
  • Assuming quoted CSS output — interpolating a bare key may drop quotes; use meta.inspect when debugging.
  • Empty maps — you get (), not an error.

💡 Best Practices

✅ Do

  • Use @use "sass:map" and map.keys()
  • Loop with @each $key in map.keys($map)
  • Read values with map.get($map, $key) inside loops
  • Pass nested maps when you need child key names
  • Prefer Dart Sass for the modern module API

❌ Don’t

  • Expect the browser to evaluate map.keys
  • Assume one call flattens an entire nested tree
  • Call map functions on the returned list
  • Confuse keys with values or get
  • Rely only on legacy map-keys in new code

Key Takeaways

Knowledge Unlocked

Five things to remember about map.keys()

Comma-separated key list in map order—great for @each generators.

5
Core concepts
📦 02

Module

sass:map

@use
🔢 03

Returns

comma-separated list

Rule
04

Scope

top-level keys only

Safe
05

Legacy

map-keys still works

Tooling

❓ Frequently Asked Questions

map.keys($map) returns a comma-separated list of all keys in the map, in the same order as the map. It does not return nested keys from child maps—only the top level of the map you pass.
map.keys returns the names (keys). map.values returns the stored values in the same order. Use keys when you need names for @each or selectors; use values when you only need the numbers/colors.
Yes. map-keys($map) is the older global name. Prefer map.keys after @use "sass:map".
No. map.keys only lists keys of the map you pass. To list keys inside a nested map, first get that nested map: map.keys(map.get($theme, "colors")).
map.keys(()) returns an empty list (). list.length of that list is 0.
Use @each $key in map.keys($map) { ... } and often map.get($map, $key) inside the loop to read each value.
Did you know?

You can also iterate a map directly with @each $key, $value in $map. map.keys shines when you only need names—or when you pass those names into other helpers and mixins.

Conclusion

map.keys() turns a map into a list of names—ideal for loops, generators, and introspection. Load it through sass:map, remember it is top-level only, and pair it with map.get or map.values as needed.

Continue with map.merge() or the Sass introduction.

Next: merge maps

Learn map.merge() to combine defaults with overrides.

map.merge() →

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