The sass:map module helps you work with maps in Sass easily. Maps are like little dictionaries that store pairs of keys and values.
sass:map module
@use "sass:map"; // Common functions: map.get($map, $key) map.set($map, $key, $value) map.has-key($map, $key) map.keys($map) map.values($map) map.merge($map1, $map2) map.remove($map, $key)
You must first @use "sass:map"; to access these functions.
Maps store data as key-value pairs, like (key: value).
primary from the $colors map.@use "sass:map"; $colors: (primary: #0055ff, secondary: #ff5500); $primary-color: map.get($colors, primary);
font-weight with value bold to the $settings map.@use "sass:map"; $settings: (font-size: 16px, line-height: 1.5); $new-settings: map.set($settings, font-weight, bold);
@use "sass:map"; $map1: (a: 1, b: 2); $map2: (b: 3, c: 4); $merged: map.merge($map1, $map2);
This Sass code uses the sass:map module to get and set colors in a map. It styles the body with background and text colors, links with the accent color, and a highlight class with a new color added to the map.
@use "sass:map"; $theme-colors: ( background: #f0f0f0, text: #333333, accent: #ff6600 ); // Get the accent color $accent-color: map.get($theme-colors, accent); // Add a new color $updated-colors: map.set($theme-colors, highlight, #00ccff); body { background-color: map.get($updated-colors, background); color: map.get($updated-colors, text); } a { color: $accent-color; } .highlight { background-color: map.get($updated-colors, highlight); padding: 0.5rem; border-radius: 0.25rem; }
Maps are great for grouping related style values, making your code cleaner.
Remember, map.set returns a new map; it does not change the original.
Use map.has-key to check if a key exists before getting its value.
The sass:map module helps manage key-value pairs in Sass.
Use it to get, set, merge, and check map values easily.
Maps keep your styles organized and easy to update.
Practice
sass:map module in Sass?Solution
Step 1: Understand the role of
Thesass:mapsass:mapmodule provides functions to work with maps, which are collections of key-value pairs.Step 2: Identify the correct purpose
Since maps store key-value pairs, the module helps manage these pairs easily in Sass stylesheets.Final Answer:
To manage key-value pairs easily in Sass stylesheets -> Option CQuick Check:
sass:map manages key-value pairs = B [OK]
- Confusing sass:map with animation or media query features
- Thinking sass:map compiles Sass to CSS
- Assuming sass:map handles layout or grid
$colors for the key primary using the sass:map module?Solution
Step 1: Recall the syntax of
The correct syntax ismap-getmap-get($map, key)without quotes around the key unless it is a string literal.Step 2: Apply to the example
Here,$colorsis the map andprimaryis the key, somap-get($colors, primary)is correct.Final Answer:
map-get($colors, primary) -> Option AQuick Check:
map-get(map, key) syntax = A [OK]
- Omitting the comma between map and key
- Swapping the order of arguments
- Adding quotes around keys unnecessarily
$theme-colors: (primary: #0055ff, secondary: #ff5500);
What will be the output CSS color for
color: map-get($theme-colors, secondary);?Solution
Step 1: Identify the map key-value pairs
The map$theme-colorshasprimary: #0055ffandsecondary: #ff5500.Step 2: Get the value for key
Usingsecondarymap-get($theme-colors, secondary)returns#ff5500.Final Answer:
#ff5500 -> Option BQuick Check:
map-get returns value for key = #ff5500 [OK]
- Confusing primary and secondary values
- Expecting the key name as output instead of value
- Assuming an error when key exists
$settings: (font-size: 16px, color: blue);
$new-settings: map-merge($settings color: red);
Solution
Step 1: Check
The correct syntax ismap-mergesyntaxmap-merge($map, (key: value))with a comma separating arguments and the new pairs inside parentheses.Step 2: Identify the error in the snippet
The snippet misses a comma between$settingsand the new paircolor: red, and the new pair is not wrapped in parentheses.Final Answer:
Missing comma between map and new pair in map-merge -> Option AQuick Check:
map-merge needs comma and parentheses = D [OK]
- Omitting commas between arguments
- Not wrapping new key-value pairs in parentheses
- Thinking keys must be quoted
$sizes: (small: 1rem, medium: 2rem, large: 3rem);
How can you create a new map
$updated-sizes that changes medium to 2.5rem but keeps other sizes unchanged using sass:map functions?Solution
Step 1: Understand how to update a map key
To update a key's value,map-mergecan merge the original map with a new map containing the updated key-value pair.Step 2: Apply
Usingmap-mergeto updatemediummap-merge($sizes, (medium: 2.5rem))replaces themediumvalue while keeping others unchanged.Final Answer:
$updated-sizes: map-merge($sizes, (medium: 2.5rem)); -> Option DQuick Check:
Use map-merge to update keys = C [OK]
- Using map-get instead of map-merge to update
- Trying map-set with wrong argument format
- Removing key before merging unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
