📖 Scenario: You are building a simple website theme. You want to keep your colors organized using Sass maps. This helps you change colors easily in one place.
🎯 Goal: Create a Sass map called $theme-colors with specific colors. Then, add a variable for a default color. Next, write a Sass function to get a color from the map by name. Finally, use that function to style the background color of the body element.
📋 What You'll Learn
Create a Sass map named $theme-colors with keys primary, secondary, and background and their exact color values.
Create a variable $default-color set to primary.
Write a Sass function get-theme-color($color-name) that returns the color from $theme-colors for the given key.
Use the get-theme-color function to set the background-color of the body selector using the $default-color.
💡 Why This Matters
🌍 Real World
Organizing colors in a Sass map helps maintain consistent themes and makes it easy to update colors in one place.
💼 Career
Knowing how to use Sass maps and functions is important for front-end developers working on scalable and maintainable CSS codebases.
Progress0 / 4 steps
1
Create the $theme-colors map
Create a Sass map called $theme-colors with these exact key-value pairs: primary: #3498db, secondary: #2ecc71, and background: #ecf0f1.
SASS
Hint
Use parentheses ( ) to create a map. Separate keys and values with a colon : and pairs with commas.
2
Add the $default-color variable
Create a variable called $default-color and set it to the string primary.
SASS
Hint
Remember to end the line with a semicolon ;.
3
Write the get-theme-color function
Write a Sass function named get-theme-color that takes one parameter $color-name. It should return the value from the $theme-colors map for the key $color-name using map.get().
SASS
Hint
Use @function to define a function and @return to send back a value.
4
Use the function to style the body
Add a body selector and set its background-color property to the result of calling get-theme-color($default-color).
SASS
Hint
Use the function call inside the property value.
Practice
(1/5)
1. What is the main purpose of the sass:map module in Sass?
easy
A. To create animations in Sass
B. To handle media queries automatically
C. To manage key-value pairs easily in Sass stylesheets
D. To compile Sass files into CSS
Solution
Step 1: Understand the role of sass:map
The sass:map module 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 C
Quick Check:
sass:map manages key-value pairs = B [OK]
Hint: Remember: sass:map is for key-value pairs management [OK]
Common Mistakes:
Confusing sass:map with animation or media query features
Thinking sass:map compiles Sass to CSS
Assuming sass:map handles layout or grid
2. Which of the following is the correct syntax to get a value from a Sass map named $colors for the key primary using the sass:map module?
easy
A. map-get($colors, primary)
B. map-get($colors primary)
C. map-get(primary, $colors)
D. map-get($colors, 'primary')
Solution
Step 1: Recall the syntax of map-get
The correct syntax is map-get($map, key) without quotes around the key unless it is a string literal.
Step 2: Apply to the example
Here, $colors is the map and primary is the key, so map-get($colors, primary) is correct.
Final Answer:
map-get($colors, primary) -> Option A
Quick Check:
map-get(map, key) syntax = A [OK]
Hint: Use map-get(map, key) with comma and no quotes for keys [OK]