Home Sass Maps Sass Maps Definition
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
Syntax @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.
Code
SCSS Examples Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).
1. get 2. @each 3. set 4. merge 5. Theme Example 1 — Official map.get Look up a key; missing keys return null (falsey).
@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")};
} .medium {
font-weight: 500;
}
.demo {
--has-extra: false;
} How It Works map.get(..., "medium") returns 500.Missing keys return null.map.has-key is a clear boolean check.Matches the official lookup sample idea.Example 2 — Official @each Over a Map Assign each key and value, then emit a rule per pair.
$icons: ("eye": "\f112", "start": "\f12e", "stop": "\f12f");
@each $name, $glyph in $icons {
.icon-#{$name}:before {
display: inline-block;
font-family: "Icon Font";
content: $glyph;
}
} .icon-eye:before {
display: inline-block;
font-family: "Icon Font";
content: "\f112";
}
.icon-start:before {
display: inline-block;
font-family: "Icon Font";
content: "\f12e";
}
.icon-stop:before {
display: inline-block;
font-family: "Icon Font";
content: "\f12f";
} How It Works @each $name, $glyph unpacks each pair.#{$name} builds the class name.$glyph becomes the content value.Matches the official Sass docs sample.Example 3 — Official map.set (Immutable) Add or replace a key; the original map stays unchanged.
@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")};
} .demo {
--extra: 900;
--bold: 900;
--orig-bold: 700;
} How It Works set adds "extra-bold" on a copy.set can also replace an existing key.$font-weights still has bold 700.Matches the official set sample idea.Example 4 — Official map.merge Combine maps; the second map wins when keys clash.
@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")};
} .demo {
--lightest: 100;
--bold: 700;
--medium: 700;
} How It Works merge joins all keys from both maps.Second map overrides shared keys.Original maps stay unchanged.Matches the official merge samples.Example 5 — Theme Map in Real CSS Store design tokens in a map and look them up in rules.
@use "sass:map";
$theme: (
"primary": #036,
"accent": #c6538c,
);
.button {
background: map.get($theme, "primary");
color: white;
&.accent {
background: map.get($theme, "accent");
}
} .button {
background: #036;
color: white;
}
.button.accent {
background: #c6538c;
} How It Works $theme holds named colors.map.get injects values into CSS.One place to change brand colors.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 .
Toolchain
Sass Support Maps and sass:map are core Sass on Dart Sass.
Feature Status Map literals (...) Supported map.get / set / mergeSupported @each over mapsSupported Maps as lists Supported
Compile with the Dart Sass CLI .
Remember
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.
Practice
Compile Notes Practical checklist:
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 What is a Sass map? A map holds unique keys paired with values, written ("key": value, …). Keys must be unique; values may repeat. Maps must use parentheses.
How do I look up a value? Use map.get($map, $key). It returns the value for that key, or null if the key is missing.
Are Sass maps mutable? No. map.set() and map.merge() return new maps. Reassign the variable if you need to keep the updated map.
Why quote map keys? Prefer quoted strings. Unquoted names like red are colors, not strings, which can cause confusing key mismatches.
How do I loop a map? Use @each $key, $value in $map { … }. Each pair is assigned to those two variables once.
Are maps also lists? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion