📖 Scenario: You are creating a simple color theme for a website using Sass. You want to store colors in a map and then use Sass's built-in map functions to access and update these colors.
🎯 Goal: Build a Sass stylesheet that defines a color map, adds a new color to the map, retrieves a color value, and uses it to style a button.
📋 What You'll Learn
Create a Sass map called $colors with three colors and their hex codes.
Add a new color to the $colors map using a built-in map function.
Retrieve the hex code of a specific color from the $colors map.
Use the retrieved color to style a button's background color.
💡 Why This Matters
🌍 Real World
Using Sass maps helps organize theme colors or settings in a clean way, making it easier to maintain and update styles in real projects.
💼 Career
Knowing how to use Sass built-in map functions is valuable for front-end developers working with CSS preprocessors to build scalable and maintainable stylesheets.
Progress0 / 4 steps
1
Create the initial color map
Create a Sass map called $colors with these exact entries: primary: #3498db, secondary: #2ecc71, and danger: #e74c3c.
SASS
Hint
Use parentheses ( ) to create a map and separate entries with commas.
2
Add a new color to the map
Use the Sass built-in map function map-merge() to add a new color warning: #f1c40f to the existing $colors map and assign it back to $colors.
SASS
Hint
Use map-merge($colors, (warning: #f1c40f)) to add the new color.
3
Retrieve a color value from the map
Use the Sass built-in map function map-get() to create a variable called $primary-color that gets the value of the primary key from the $colors map.
SASS
Hint
Use map-get($colors, primary) to get the primary color value.
4
Use the retrieved color to style a button
Create a CSS rule for a .btn-primary class that sets the background-color property to the $primary-color variable.
SASS
Hint
Use .btn-primary { background-color: $primary-color; } to style the button.
Practice
(1/5)
1. Which Sass built-in map function is used to check if a key exists in a map? map-get(), map-has-key(), map-keys(), or map-values()?
easy
A. map-has-key()
B. map-get()
C. map-keys()
D. map-values()
Solution
Step 1: Understand the purpose of each function
map-get() retrieves a value, map-keys() lists keys, map-values() lists values, and map-has-key() checks if a key exists.
Step 2: Identify the function that checks key existence
The function map-has-key() returns true or false if a key is in the map.
Final Answer:
map-has-key() -> Option A
Quick Check:
Check key presence = map-has-key() [OK]
Hint: To check keys, use map-has-key() [OK]
Common Mistakes:
Confusing map-get() with map-has-key()
Using map-keys() to check key existence
Thinking map-values() checks keys
2. Which of the following is the correct syntax to get the value for key "color" from a Sass map named $theme?
easy
A. map-get($theme, color)
B. map-get(color, $theme)
C. map-get($theme, $color)
D. map-get($theme, "color")
Solution
Step 1: Recall map-get() syntax
The correct syntax is map-get($map, $key) where the key is a string or identifier.
Step 2: Identify correct key format
Keys that are strings must be quoted with double or single quotes. Here, "color" is correct.
Final Answer:
map-get($theme, "color") -> Option D
Quick Check:
Correct syntax uses map-get($map, "key") [OK]
Hint: Use quotes around string keys in map-get() [OK]
Common Mistakes:
Omitting quotes around string keys
Swapping map and key order
Using a variable ($color) instead of a quoted string key