Sass Maps
A map is a collection of key/value pairs, written in parentheses: (key: value, key2: value2). Maps are how Sass projects store design tokens — colour palettes, spacing scales, breakpoints, z-index layers — in one structured place. The sass:map module provides functions to read and change them.
$breakpoints: (
sm: 576px,
md: 768px,
lg: 1024px,
);Keys are usually unquoted names, but they can be any Sass value. A trailing comma is allowed.
Reading values with map.get
@use "sass:map";
$palette: (
primary: #cc6699,
secondary: #4a3f6b,
surface: #fff8fb,
);
.banner {
background: map.get($palette, surface);
border-left: 6px solid map.get($palette, primary);
color: map.get($palette, secondary);
}
.missing {
color: map.get($palette, tertiary); // null → declaration omitted
padding: 8px;
}.banner {
background: #fff8fb;
border-left: 6px solid #cc6699;
color: #4a3f6b;
}
.missing {
padding: 8px;
}
map.get returns null for a missing key, and a property set to null is left out of the CSS.
The sass:map functions
| Function | Returns |
|---|---|
map.get($map, $key) |
The value, or null |
map.has-key($map, $key) |
true / false |
map.keys($map) |
A list of keys |
map.values($map) |
A list of values |
map.merge($map1, $map2) |
A new map; keys in $map2 win |
map.remove($map, $keys...) |
A new map without those keys |
map.set($map, $key, $value) |
A new map with the key set |
map.deep-merge($a, $b) |
Like merge, but recursive for nested maps |
Note: Maps are immutable.
map.mergeandmap.setreturn a new map — assign it back:$m: map.set($m, key, value);.
Merging maps: theming defaults
@use "sass:map";
$defaults: (radius: 4px, shadow: none, accent: #555);
$overrides: (accent: #cc6699, radius: 12px);
$config: map.merge($defaults, $overrides);
.widget {
border-radius: map.get($config, radius);
box-shadow: map.get($config, shadow);
outline-color: map.get($config, accent);
}
.debug::after {
content: "#{map.keys($config)}";
}.widget {
border-radius: 12px;
box-shadow: none;
outline-color: #cc6699;
}
.debug::after {
content: "radius, shadow, accent";
}
Looping over a map
Combine maps with @each to generate classes:
$z-layers: (
dropdown: 100,
sticky: 200,
modal: 300,
toast: 400,
);
@each $name, $z in $z-layers {
.z-#{$name} {
z-index: $z;
}
}.z-dropdown {
z-index: 100;
}
.z-sticky {
z-index: 200;
}
.z-modal {
z-index: 300;
}
.z-toast {
z-index: 400;
}
Nested maps
A value can itself be a map. map.get accepts several keys to dig into nested maps:
@use "sass:map";
$theme: (
colors: (
text: #222,
brand: (base: #cc6699, dark: #993366),
),
radius: 8px,
);
.link {
color: map.get($theme, colors, brand, base);
&:hover {
color: map.get($theme, colors, brand, dark);
}
}.link {
color: #cc6699;
}
.link:hover {
color: #993366;
}
A safe getter with an error
When a key is required, fail loudly instead of silently outputting nothing:
@use "sass:map";
@function token($key) {
@if not map.has-key($palette, $key) {
@error "Unknown token `#{$key}`. Expected one of: #{map.keys($palette)}";
}
@return map.get($palette, $key);
}Tip: Store raw design decisions in maps, then generate CSS (classes or custom properties) from them. Changing the map updates everything that depends on it.
Test yourself
What does map.get((a: 1, b: 2), c) return?
- 0
- null
- An error
- "c"
After $m: (a: 1); $n: map.set($m, b, 2);, what is $m?
- (a: 1, b: 2)
- (a: 1)
- (b: 2)
- null
In map.merge($a, $b), which value wins if both maps have the same key?
- The one from $a
- The one from $b
- Sass throws an error
- They are added together