Sass Maps

Beginner
⏱️ 14 min read
📚 Updated: Jul 2026
🎯 5 Examples
Keys & values

What You’ll Learn

Sass maps store key/value pairs for easy lookups. This page covers writing maps, quoted keys, map.get / set / merge, @each loops, immutability, and five compiled examples.

01

Pairs

key : value

02

Lookup

map.get

03

Update

set / merge

04

@each

Loop pairs

05

Quoted keys

Safer default

06

Practice

5 examples

What Are Sass Maps?

Official docs: maps hold pairs of keys and values so you can look up a value by its key. They are written (key: value, …). Keys must be unique; values may be duplicated. Unlike lists, maps must use parentheses. An empty map is ().

  • Any Sass value can be a key; equality uses ==.
  • Maps are not valid CSS by themselves—use helpers or loops to emit CSS.
  • Every map also counts as a list of two-element key/value lists.
  • Prefer quoted string keys to avoid color-name traps.
💡
Beginner tip

Think of a map as a design-token dictionary: names on the left, concrete CSS values on the right. Look up one token with map.get, or generate many rules with @each.

📝 Syntax

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

$empty: (); // empty map (also an empty list)

📄 Prefer Quoted Keys

Official docs: most of the time, use quoted strings for keys. Some values that look like unquoted strings—especially color names like red—are other types. Quoting avoids confusing lookup bugs later.

🧮 Using Maps

Load @use "sass:map" for helpers:

NeedFunction
Get a valuemap.get($map, $key)
Set / replacemap.set($map, $key, $value)
Combine mapsmap.merge($map1, $map2)
Check a keymap.has-key($map, $key)
All keys / valuesmap.keys() / map.values()

Loop pairs with @each: @each $key, $value in $map { … }.

🔗 Maps Count as Lists

Official docs: every map is also a list containing a two-element list for each pair. For example, (1: 2, 3: 4) counts as (1 2, 3 4). That is why empty () is both an empty map and an empty list.

🔒 Immutability

Official docs: map contents never change in place. map.set and map.merge return new maps. Reassign when you need updated state: $config: map.merge($config, ("theme": dark));

⚡ Quick Reference

GoalCode
Write a map("sm": 480px, "md": 768px)
Look upmap.get($map, "md")
Add a keymap.set($map, "lg", 1024px)
Mergemap.merge($a, $b) (second wins on clashes)
Loop pairs@each $k, $v in $map { … }
Missing keymap.getnull (falsey)

Examples Gallery

Each example turns map data into CSS or inspectable values. Open View Compiled CSS for verified output.

📚 Getting Started

Look up values and generate rules from pairs.

Example 1 — map.get, Keys & Values

Official-docs style font-weight map with lookup helpers.

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

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

.lookup {
  --medium: #{meta.inspect(map.get($font-weights, "medium"))};
  --missing: #{meta.inspect(map.get($font-weights, "extra-bold"))};
  --has: #{meta.inspect(map.has-key($font-weights, "bold"))};
  --keys: #{meta.inspect(map.keys($font-weights))};
  --vals: #{meta.inspect(map.values($font-weights))};
  --pair: #{meta.inspect(list.nth($font-weights, 1))};
}

How It Works

A missing key returns null. Because maps are lists of pairs, list.nth($map, 1) returns the first key/value list.

Example 2 — Loop Icons with @each

Keys become class names; values become glyph content.

styles.scss
$icons: ("eye": "\f112", "start": "\f12e", "stop": "\f12f");

@each $name, $glyph in $icons {
  .icon-#{$name}:before {
    display: inline-block;
    font-family: "Icon Font";
    content: $glyph;
  }
}

How It Works

Add one map entry and Sass writes the matching ::before rule—no copy-paste classes.

📈 Set, Merge & Design Tokens

Immutable updates, breakpoints, and semantic color maps.

Example 3 — map.set & map.merge

Add keys, replace values, and combine maps (second map wins on clashes).

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

$font-weights: ("regular": 400, "medium": 500, "bold": 700);
$light-weights: ("lightest": 100, "light": 300);
$heavy-weights: ("medium": 500, "bold": 700);
$weights: ("light": 300, "medium": 500);

.set-merge {
  --set-new: #{meta.inspect(map.set($font-weights, "extra-bold", 900))};
  --set-replace: #{meta.inspect(map.set($font-weights, "bold", 900))};
  --merged: #{meta.inspect(map.merge($light-weights, $heavy-weights))};
  --override: #{meta.inspect(map.merge($weights, ("medium": 700)))};
  --original: #{meta.inspect($font-weights)};
}

How It Works

--original proves map.set did not mutate $font-weights. Merge puts the second map’s values on shared keys.

Example 4 — Breakpoint Map → Media Queries

Generate container max-widths from named breakpoints, plus a one-off map.get.

styles.scss
@use "sass:map";

$breakpoints: (
  "sm": 480px,
  "md": 768px,
  "lg": 1024px,
);

@each $name, $width in $breakpoints {
  @media (min-width: $width) {
    .container-#{$name} {
      max-width: $width;
    }
  }
}

.btn {
  font-weight: map.get(("regular": 400, "bold": 700), "bold");
}

How It Works

One map drives every breakpoint rule. Looking up "bold" returns 700 for the button.

Example 5 — Semantic Tone Tokens

Turn a color map into badge and text utility classes.

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

$tones: (
  "success": #2f9e64,
  "warning": #e6a700,
  "danger": #d64550,
);

@each $name, $color in $tones {
  .badge-#{$name} {
    background: $color;
    color: #fff;
  }

  .text-#{$name} {
    color: $color;
  }
}

.quote-keys {
  --ok: #{meta.inspect(map.get(("red": #c00, "blue": #00c), "red"))};
}

How It Works

Quoted "red" is a string key. An unquoted red key would be a color value instead—exactly why quoting keys is the safer habit.

🚀 Real-World Use Cases

  • Design tokens — colors, weights, radii, z-index layers.
  • Breakpoints — named widths that drive media queries.
  • Icon fonts — name → glyph maps.
  • Theme config — merge defaults with overrides.
  • Component APIs — look up options with map.get.

🧠 How Compilation Works

1

Parse the pairs

Read keys and values inside parentheses.

Parse
2

Look up or loop

Use map.get / set / merge, or @each over each pair.

Work
3

Emit CSS values

Maps themselves disappear; only derived styles remain.

Serialize
4

CSS ships

Browsers never see the map—only the CSS you generated.

⚠️ Common Pitfalls

  • Unquoted color-like keysred is a color, not the string "red".
  • Expecting mutationmap.set returns a new map.
  • Putting a map in a CSS property — maps are not valid CSS values.
  • Forgetting parentheses — maps must be wrapped in ().
  • Ignoring null from map.get — missing keys are falsey; handle them.

💡 Best Practices

✅ Do

  • Quote string keys by default
  • Keep token maps as the single source of truth
  • Use @each $k, $v in $map to generate utilities
  • Reassign after set / merge when updating config
  • Prefer map.get over hard-coded duplicate values

❌ Don’t

  • Use unquoted color names as keys
  • Assume map.set changes the original variable
  • Emit raw maps into CSS properties
  • Scatter the same token hex in many files
  • Skip checks when a key might be missing

Key Takeaways

Knowledge Unlocked

Five things to remember about Sass maps

Key/value tokens, safe lookups, and immutable updates.

5
Core concepts
🔍 02

map.get

or null

Lookup
🔄 03

@each

loop pairs

Generate
🔒 04

Immutable

set = new map

Safety
05

Quote keys

avoid traps

Habit

❓ Frequently Asked Questions

A map holds unique keys paired with values, written ("key": value, …). Keys must be unique; values may repeat. Maps must use parentheses.
Use map.get($map, $key). It returns the value for that key, or null if the key is missing.
No. map.set() and map.merge() return new maps. Reassign the variable if you need to keep the updated map.
Prefer quoted strings. Unquoted names like red are colors, not strings, which can cause confusing key mismatches.
Use @each $key, $value in $map { … }. Each pair is assigned to those two variables once.
Yes. Every map counts as a list of two-element key/value lists. An empty map () is also an empty list.
Did you know?

Official Sass docs point out that an empty map () is written the same as an empty list—because a map is a list of key/value pairs under the hood.

Conclusion

Sass maps are the go-to structure for named design tokens. Quote your keys, look up with map.get, update immutably with set / merge, and generate CSS with @each.

Continue with Sass Booleans or map.get().

Next: Sass Booleans

Learn true/false, and/or/not, truthiness, and @if.

Sass Booleans →

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