Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Spacing Scale Generation with Sass
📖 Scenario: You are creating a consistent spacing scale for a website using Sass. This scale will help keep margins and paddings uniform across the site.
🎯 Goal: Build a Sass map called $spacing-scale with specific spacing values, then create a variable for the base spacing unit, generate a new map with doubled spacing values, and finally output CSS custom properties for these spacing values.
📋 What You'll Learn
Create a Sass map named $spacing-scale with keys small, medium, and large and values 0.5rem, 1rem, and 2rem respectively.
Create a variable named $base-spacing and set it to 1rem.
Generate a new Sass map named $double-spacing by multiplying each value in $spacing-scale by 2.
Output CSS custom properties for each key in $double-spacing inside the :root selector.
💡 Why This Matters
🌍 Real World
Spacing scales help keep website design consistent and easy to update by centralizing spacing values.
💼 Career
Front-end developers use Sass maps and loops to manage design tokens like spacing, colors, and fonts efficiently.
Progress0 / 4 steps
1
Create the initial spacing scale map
Create a Sass map called $spacing-scale with these exact entries: small: 0.5rem, medium: 1rem, and large: 2rem.
SASS
Hint
Use parentheses ( ) to create a Sass map and separate key-value pairs with commas.
2
Add a base spacing variable
Create a variable called $base-spacing and set it to 1rem.
SASS
Hint
Variables in Sass start with $. Assign the value with a colon and end with a semicolon.
3
Generate doubled spacing map
Create a new Sass map called $double-spacing by multiplying each value in $spacing-scale by 2 using a @each loop and map-merge().
SASS
Hint
Use @each $key, $value in $spacing-scale to loop. Use map-merge() to add to a map.
4
Output CSS custom properties for doubled spacing
Write CSS inside the :root selector that outputs custom properties for each key in $double-spacing using a @each loop. The property names should be --spacing- followed by the key, and the values should be the doubled spacing values.
SASS
Hint
Use :root selector and inside it use @each $key, $value in $double-spacing. Output custom properties with --spacing-#{$key}: #{$value};.
Practice
(1/5)
1. What is the main purpose of generating a spacing scale in Sass?
easy
A. To create consistent and reusable space sizes across a website
B. To change font colors dynamically
C. To add animations to elements
D. To optimize image loading speed
Solution
Step 1: Understand spacing scale concept
A spacing scale is a set of predefined space sizes used consistently in design.
Step 2: Identify its purpose in Sass
It helps keep spacing uniform and easy to manage by reusing values.
Final Answer:
To create consistent and reusable space sizes across a website -> Option A
Quick Check:
Spacing scale = consistent spacing [OK]
Hint: Spacing scale means consistent space sizes reused [OK]
Common Mistakes:
Confusing spacing scale with color or animation features
Thinking spacing scale changes fonts or images
2. Which Sass syntax correctly defines a map for spacing scale with keys 1, 2, 3 and values 0.25rem, 0.5rem, 1rem?
easy
A. $spacing-scale = {1: 0.25rem, 2: 0.5rem, 3: 1rem};
A. Cannot multiply number by unit directly in map-merge key-value
B. map-merge syntax is incorrect; keys must be strings
C. The function does not initialize $scale as a map
D. Using $i: $i * 0.25rem inside map-merge is invalid syntax
Solution
Step 1: Analyze map-merge usage
map-merge expects a map and a map as arguments. The second argument must be a map with key-value pairs.
Step 2: Check key-value pair syntax
$i: $i * 0.25rem is invalid inside map-merge because $i: is not a valid map literal key syntax. It should be ($i: $i * 0.25rem) wrapped in parentheses.
Final Answer:
Using $i: $i * 0.25rem inside map-merge is invalid syntax -> Option D
Quick Check:
map-merge needs proper map syntax [OK]
Hint: Wrap key-value pair in parentheses for map-merge [OK]
Common Mistakes:
Forgetting parentheses around key-value pair in map-merge
Trying to multiply unitless number by unit incorrectly
Not initializing $scale as empty map ()
5. You want to create a spacing scale in Sass that doubles each step starting from 0.25rem for 5 steps (0.25rem, 0.5rem, 1rem, 2rem, 4rem). Which function correctly generates this scale as a map with keys 1 to 5?
hard
A. @function generate-scale($steps) {
$scale: ();
$value: 0.25rem;
@for $i from 1 through $steps {
$scale: map-merge($scale, ($i: $value));
$value: $value * 2;
}
@return $scale;
}
B. @function generate-scale($steps) {
$scale: ();
@for $i from 1 through $steps {
$scale: map-merge($scale, ($i: 0.25rem * $i));
}
@return $scale;
}