📖 Scenario: You are creating a style guide for a website. You want to organize colors by categories like primary, secondary, and neutral using Sass maps. This helps keep your colors neat and easy to update.
🎯 Goal: Build a Sass map called $color-palette that groups colors by category. Then, create a variable $primary-color that picks the main primary color from the map. Finally, use the primary color in a CSS rule for the body background.
📋 What You'll Learn
Create a Sass map named $color-palette with keys primary, secondary, and neutral.
Each key should map to another map with exactly two colors: main and light.
Create a variable $primary-color that gets the main color from the primary map inside $color-palette.
Use $primary-color as the background color for the body selector.
💡 Why This Matters
🌍 Real World
Organizing colors in grouped maps helps teams maintain consistent design systems and makes updating colors easier across large projects.
💼 Career
Knowing how to use Sass maps is valuable for front-end developers working with CSS preprocessors to write clean, scalable stylesheets.
Progress0 / 4 steps
1
Create the color palette map
Create a Sass map called $color-palette with these exact keys and nested maps: primary with main: #005f73 and light: #0a9396, secondary with main: #94d2bd and light: #e9d8a6, and neutral with main: #ee9b00 and light: #ca6702.
SASS
Hint
Remember to use parentheses ( ) for maps and commas , between entries.
2
Create a variable for the primary main color
Create a variable called $primary-color that gets the main color from the primary map inside $color-palette using map-get twice.
SASS
Hint
Use map-get twice: first to get the primary map, then to get main color.
3
Use the primary color in a CSS rule
Write a CSS rule for the body selector that sets background-color to the variable $primary-color.
SASS
Hint
Use the body selector and set background-color to $primary-color.
4
Add a hover effect using the primary light color
Add a CSS rule for body:hover that changes the background-color to the light color from the primary map inside $color-palette. Use map-get to get this color directly inside the rule.
SASS
Hint
Use body:hover and set background-color using map-get twice to get the light primary color.
Practice
(1/5)
1. What is the main purpose of using maps in Sass?
easy
A. To write JavaScript inside Sass files
B. To group related values together for easier management
C. To create animations in CSS
D. To add comments in stylesheets
Solution
Step 1: Understand what maps do in Sass
Maps store multiple related values under named keys, making styles organized.
Step 2: Compare options with map purpose
Only grouping related values matches the purpose of maps.
Final Answer:
To group related values together for easier management -> Option B
Quick Check:
Maps group values = C [OK]
Hint: Maps group related values by keys for easy access [OK]
Common Mistakes:
Thinking maps create animations
Confusing maps with comments
Believing maps run JavaScript
2. Which of the following is the correct syntax to define a Sass map named $colors with keys primary and secondary?
easy
A. $colors: (primary: #ff0000, secondary: #00ff00);
B. $colors = {primary: #ff0000, secondary: #00ff00};
C. $colors: [primary: #ff0000, secondary: #00ff00];
D. $colors: primary => #ff0000, secondary => #00ff00;
Solution
Step 1: Recall Sass map syntax
Sass maps use parentheses with key: value pairs separated by commas.
Step 2: Check each option's syntax
$colors: (primary: #ff0000, secondary: #00ff00); uses correct parentheses and colon syntax; others use invalid symbols or brackets.
Final Answer:
$colors: (primary: #ff0000, secondary: #00ff00); -> Option A
Quick Check:
Maps use (key: value) pairs [OK]
Hint: Use parentheses and colons for map syntax [OK]
B. Map must use square brackets instead of parentheses
C. map.get() function does not exist in Sass
D. Keys in map.get() should not be quoted
Solution
Step 1: Check map key usage in map.get()
In Sass, map keys are identifiers and should not be quoted when used in map.get().
Step 2: Verify other options
Keys can be strings or identifiers but quoting keys in map.get() is incorrect; map.get() exists; maps use parentheses.
Final Answer:
Keys in map.get() should not be quoted -> Option D
Quick Check:
Use unquoted keys in map.get() [OK]
Hint: Do not quote keys inside map.get() [OK]
Common Mistakes:
Quoting keys in map.get()
Using wrong brackets for maps
Thinking map.get() is invalid
5. You want to create a Sass map $button-styles that groups background, color, and border-radius for buttons. Which code correctly defines this map and accesses the color value?