Spacing scale helps keep consistent space sizes in your design. It makes your layout neat and balanced.
Spacing scale generation in SASS
Start learning this pattern below
Jump into concepts and practice - no test required
@function spacing-scale($step) { @return $step * 0.5rem; } // Usage example: .element { margin-bottom: spacing-scale(4); // 2rem }
The function takes a number and returns a space size in rem units.
Using rem units helps keep spacing relative to the root font size, making it responsive.
@function spacing-scale($step) { @return $step * 0.25rem; } .box { padding: spacing-scale(2); // 0.5rem }
@function spacing-scale($step) { @return $step * 1rem; } .container { margin-top: spacing-scale(3); // 3rem }
$spacing-values: (0: 0, 1: 0.5rem, 2: 1rem, 3: 1.5rem); .element { padding: map-get($spacing-values, 2); // 1rem }
This code defines a spacing scale function that multiplies the step by 0.5rem. The container uses padding and margin from the scale to keep spacing consistent. The text inside also uses the scale for margin.
@function spacing-scale($step) { @return $step * 0.5rem; } .container { padding: spacing-scale(4); margin-bottom: spacing-scale(2); background-color: #e0f7fa; border: 1px solid #00796b; width: 300px; font-family: Arial, sans-serif; } .text { margin-top: spacing-scale(3); color: #004d40; font-size: 1rem; }
Use rem units for spacing to keep it scalable with user settings.
Changing the base multiplier in the function updates all spacing at once.
Keep spacing steps consistent to maintain visual rhythm.
Spacing scale helps create consistent and easy-to-manage space sizes.
Use a function or map in Sass to generate spacing values.
Use rem units for responsive and accessible spacing.
Practice
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 AQuick Check:
Spacing scale = consistent spacing [OK]
- Confusing spacing scale with color or animation features
- Thinking spacing scale changes fonts or images
Solution
Step 1: Recall Sass map syntax
Sass maps use parentheses () with key: value pairs separated by commas.Step 2: Check each option
$spacing-scale: (1: 0.25rem, 2: 0.5rem, 3: 1rem); uses correct syntax with colons and parentheses. Others use invalid symbols or brackets.Final Answer:
$spacing-scale: (1: 0.25rem, 2: 0.5rem, 3: 1rem); -> Option CQuick Check:
Sass map syntax = parentheses + colon [OK]
- Using = instead of : for assignment
- Using square brackets [] instead of parentheses ()
- Using => instead of : for key-value pairs
$spacing-scale: (1: 0.25rem, 2: 0.5rem, 3: 1rem);
@mixin spacing($size) {
margin: map-get($spacing-scale, $size);
}
.box {
@include spacing(2);
}What CSS will be generated for the
.box class?Solution
Step 1: Understand map-get usage
map-get($spacing-scale, 2) returns the value for key 2, which is 0.5rem.Step 2: Apply mixin to .box
The mixin sets margin to 0.5rem for .box.Final Answer:
.box { margin: 0.5rem; } -> Option BQuick Check:
map-get key 2 = 0.5rem [OK]
- Confusing keys and values in map-get
- Assuming mixin sets padding instead of margin
- Mixing up rem values for keys
@function generate-spacing($steps) {
$scale: ();
@for $i from 1 through $steps {
$scale: map-merge($scale, $i: $i * 0.25rem);
}
@return $scale;
}
$spacing-scale: generate-spacing(3);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 DQuick Check:
map-merge needs proper map syntax [OK]
- Forgetting parentheses around key-value pair in map-merge
- Trying to multiply unitless number by unit incorrectly
- Not initializing $scale as empty map ()
Solution
Step 1: Understand doubling logic
The value starts at 0.25rem and doubles each step: multiply by 2 each loop.Step 2: Check each function
@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; } correctly initializes $value, merges map with ($i: $value), then doubles $value by multiplying by 2.Step 3: Identify errors in others
@function generate-scale($steps) { $scale: (); @for $i from 1 through $steps { $scale: map-merge($scale, ($i: 0.25rem * $i)); } @return $scale; } multiplies by $i (linear, not doubling). @function generate-scale($steps) { $scale: (); $value: 0.25rem; @each $i in 1 2 3 4 5 { $scale: map-merge($scale, ($i: $value)); $value: $value + 0.25rem; } @return $scale; } uses @each with addition (not doubling). @function generate-scale($steps) { $scale: (); $value: 0.25rem; @for $i from 1 through $steps { $scale: map-merge($scale, ($i: $value)); $value: $value + $value; } @return $scale; } adds $value to itself (works but less clear than multiply by 2).Final Answer:
Option A function correctly generates doubling scale -> Option AQuick Check:
Multiply by 2 each step = doubling scale [OK]
- Using addition instead of multiplication for doubling
- Multiplying by loop index instead of doubling
- Using @each instead of @for for numeric loops
