Maps are Sass's key-value data structure, ideal for representing design tokens like breakpoints, spacing scales, or color palettes. This lesson covers map syntax and the sass:map module functions for reading, updating, and merging maps.
What Is a Sass Map?
A map associates keys with values, written as (key: value, key: value). Maps are commonly used to represent structured configuration, like breakpoints, a spacing scale, or a full color palette, in one central, named place.
Maps always use parentheses with comma-separated key: value pairs.
map.get() retrieves a value by key, returning null if the key doesn't exist.
map.has-key() checks whether a key exists before you try to use its value.
map.keys() and map.values() return the map's keys or values as separate lists.
Sass Maps Cheatsheet
The sass:map module functions you'll use to build design tokens and configuration.
Function
Example
Result
map.get()
map.get((sm: 8px), sm)
8px
map.has-key()
map.has-key((sm: 8px), md)
false
map.keys()
map.keys((sm: 8px, md: 16px))
sm, md
map.values()
map.values((sm: 8px, md: 16px))
8px, 16px
map.merge()
map.merge($a, $b)
Combined map, $b's keys win on conflict
map.remove()
map.remove($map, sm)
Map without the sm key
map.set()
map.set($map, lg, 992px)
New map with lg added or updated
Reading and Checking Map Values
Always check for a key's existence with map.has-key() before assuming map.get() returned a meaningful value, since a missing key silently returns null rather than raising an error.
map.merge() combines two maps into a new one, with the second map's values winning on any overlapping keys, a common pattern for layering a user's custom configuration on top of a library's defaults.
@each can destructure a map's entries directly into a key and value variable, making it easy to generate a rule for every entry, exactly as shown in the Colors lesson's palette example.
Generates .p-sm, .p-md, and .p-lg, one rule per map entry.
Common Mistakes
Calling map.get() on a missing key and using the resulting null value without checking it first, producing a broken declaration.
Forgetting that map keys must be unique; duplicate keys in a map literal silently keep only the last one.
Using map.merge() in the wrong argument order, overwriting the values you actually meant to keep.
Building deeply nested maps (maps of maps of maps) that become hard to read and query without small helper functions.
Key Takeaways
Sass maps store key-value pairs using (key: value, ...) syntax.
map.get(), map.has-key(), map.keys(), and map.values() cover most map reading needs.
map.merge() layers one map's values on top of another, ideal for configurable defaults.
@each $key, $value in $map is the standard way to iterate over a map's entries.
Pro Tip
Model your design tokens (breakpoints, spacing, colors, typography scale) as maps from day one, even in a small project, it makes the eventual move to a formal design-token architecture (covered later in this course) far less disruptive.
You now understand how Sass maps model structured, key-value configuration. Next, learn Sass control rules for adding logic to your stylesheets.