Sass Maps

Beginner
9 min read
Updated: Nov 2026
5 examples
Dart Sass

Definition and Usage

Maps hold key/value pairs, written ("key": value, ...). Keys must be unique; values may repeat. Parentheses are required.

Not a CSS value.

Maps never appear directly in CSS. Look values up with map.get() (or loop with @each) and emit normal CSS.

Quote your keys.

Prefer "red" over bare red—unquoted names can be colors, not strings.

Immutable.

map.set() and map.merge() return new maps. Reassign the variable to keep updates.

Syntax

SCSS
@use "sass:map";

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

map.get($font-weights, "medium");              // 500
map.get($font-weights, "extra-bold");           // null
map.set($font-weights, "extra-bold", 900);      // new map
map.merge($a, $b);                              // combine maps

@each $key, $value in $font-weights { ... }

Pattern: define a map → read with map.get → update with map.set / map.merge → loop pairs with @each.

Common Patterns

Everyday map tools:

PatternResult idea
("a": 1, "b": 2)Map literal (parentheses required)
map.get($map, $key)Value, or null if missing
map.set($map, $key, $val)New map with that key set
map.merge($a, $b)Combine maps ($b wins on clashes)
@each $k, $v in $mapLoop every pair
()Empty map (also an empty list)

SCSS Examples

Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).

Example 1 — Official map.get

Look up a key; missing keys return null (falsey).

SCSS
@use "sass:map";

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

.medium {
  font-weight: map.get($font-weights, "medium");
}

.demo {
  --has-extra: #{map.has-key($font-weights, "extra-bold")};
}

How It Works

  1. map.get(..., "medium") returns 500.
  2. Missing keys return null.
  3. map.has-key is a clear boolean check.
  4. Matches the official lookup sample idea.

Example 2 — Official @each Over a Map

Assign each key and value, then emit a rule per pair.

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

  1. @each $name, $glyph unpacks each pair.
  2. #{$name} builds the class name.
  3. $glyph becomes the content value.
  4. Matches the official Sass docs sample.

Example 3 — Official map.set (Immutable)

Add or replace a key; the original map stays unchanged.

SCSS
@use "sass:map";

$font-weights: ("regular": 400, "medium": 500, "bold": 700);
$updated: map.set($font-weights, "extra-bold", 900);
$replaced: map.set($font-weights, "bold", 900);

.demo {
  --extra: #{map.get($updated, "extra-bold")};
  --bold: #{map.get($replaced, "bold")};
  --orig-bold: #{map.get($font-weights, "bold")};
}

How It Works

  1. set adds "extra-bold" on a copy.
  2. set can also replace an existing key.
  3. $font-weights still has bold 700.
  4. Matches the official set sample idea.

Example 4 — Official map.merge

Combine maps; the second map wins when keys clash.

SCSS
@use "sass:map";

$light-weights: ("lightest": 100, "light": 300);
$heavy-weights: ("medium": 500, "bold": 700);
$merged: map.merge($light-weights, $heavy-weights);

$weights: ("light": 300, "medium": 500);
$over: map.merge($weights, ("medium": 700));

.demo {
  --lightest: #{map.get($merged, "lightest")};
  --bold: #{map.get($merged, "bold")};
  --medium: #{map.get($over, "medium")};
}

How It Works

  1. merge joins all keys from both maps.
  2. Second map overrides shared keys.
  3. Original maps stay unchanged.
  4. Matches the official merge samples.

Example 5 — Theme Map in Real CSS

Store design tokens in a map and look them up in rules.

SCSS
@use "sass:map";

$theme: (
  "primary": #036,
  "accent": #c6538c,
);

.button {
  background: map.get($theme, "primary");
  color: white;

  &.accent {
    background: map.get($theme, "accent");
  }
}

How It Works

  1. $theme holds named colors.
  2. map.get injects values into CSS.
  3. One place to change brand colors.
  4. Common pattern for design systems.

Edge Cases & Pitfalls

Most map bugs are unquoted keys or forgetting to reassign after set/merge.

quotes

Quote string keys

Bare red is a color. Use "red" when you mean a string key.

null

Missing keys return null

Check with map.has-key() or test the result in @if.

immutable

Reassign after set/merge

$map: map.set($map, $k, $v); keeps the new map.

lists

Maps also count as lists

Each pair is a two-element list. Empty () is both an empty map and an empty list. See lists.

Sass Support

Maps and sass:map are core Sass on Dart Sass.

FeatureStatus
Map literals (...)Supported
map.get / set / mergeSupported
@each over mapsSupported
Maps as listsSupported

Compile with the Dart Sass CLI.

Key Takeaways

  • Maps store unique keys with values inside (...).
  • map.get looks up; missing keys are null.
  • @each $k, $v loops every pair.
  • Immutable—reassign after set / merge.

One line: keep config in a map, look values up with map.get, and generate CSS with @each.

Compile Notes

Practical checklist:

checklist.txt
1. Write maps with parentheses and quoted keys
2. Look up values with map.get($map, $key)
3. Loop pairs with @each $key, $value in $map
4. Update with map.set / map.merge (then reassign)
5. Remember maps are not valid CSS by themselves

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.

Next: Sass Booleans

Use true / false (and map.has-key results) with @if.

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.

8 people found this page helpful