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
Component Variant Generation with Sass
📖 Scenario: You are building a button component for a website. The button should have different color styles depending on its variant: primary, secondary, and danger. You want to use Sass to generate these variants easily and keep your CSS clean.
🎯 Goal: Create a Sass setup that generates CSS classes for .btn-primary, .btn-secondary, and .btn-danger with different background colors and text colors using a Sass map and a loop.
📋 What You'll Learn
Create a Sass map called $btn-colors with keys primary, secondary, and danger and their respective color values.
Create a variable called $btn-base-color for the default text color.
Use a @each loop to generate CSS classes for each button variant.
Each generated class should have a background color from the map and the text color from $btn-base-color.
💡 Why This Matters
🌍 Real World
Web developers often need to create multiple style variants of components like buttons. Using Sass maps and loops helps generate these variants efficiently without repeating code.
💼 Career
Knowing how to use Sass for component variant generation is a valuable skill for front-end developers working on scalable and maintainable CSS codebases.
Progress0 / 4 steps
1
Create the button colors map
Create a Sass map called $btn-colors with these exact entries: primary: #007bff, secondary: #6c757d, and danger: #dc3545.
SASS
Hint
Use parentheses ( ) to create a Sass map and separate entries with commas.
2
Add the base text color variable
Create a variable called $btn-base-color and set it to #ffffff for white text color.
SASS
Hint
Use $btn-base-color: #ffffff; to set the white color.
3
Generate button variant classes with a loop
Use a @each loop with variables $name and $color to iterate over $btn-colors. Inside the loop, create a CSS class named .btn-#{$name} that sets background-color to $color and color to $btn-base-color.
SASS
Hint
Use @each $name, $color in $btn-colors { ... } and string interpolation .btn-#{$name} for class names.
4
Add a base button style
Add a CSS class .btn that sets padding to 0.5rem 1rem, border-radius to 0.25rem, and font-weight to 600.
SASS
Hint
Use standard CSS property syntax inside the .btn class.
Practice
(1/5)
1. What is the main purpose of component variant generation in Sass?
easy
A. To create multiple style versions of the same component easily
B. To write JavaScript inside Sass files
C. To compile Sass into JavaScript
D. To remove unused CSS automatically
Solution
Step 1: Understand component variants
Component variants allow creating different styles for the same element, like buttons with different colors.
Step 2: Identify the main purpose
The main goal is to generate these style versions easily and keep code organized.
Final Answer:
To create multiple style versions of the same component easily -> Option A
C. Missing interpolation for $name in the selector
D. Mixin cannot be included with parameters
Solution
Step 1: Check selector syntax
Variables inside selectors need interpolation with '#{}'. Here '.btn-$name' misses '#{}'.
Step 2: Understand interpolation usage
Correct syntax is '.btn-#{$name}' to insert the variable value.
Final Answer:
Missing interpolation for $name in the selector -> Option C
Quick Check:
Use '#{}' to insert variables in selectors [OK]
Hint: Use #{} around variables in selectors [OK]
Common Mistakes:
Forgetting interpolation syntax
Thinking variables can't be in selectors
Misusing mixin parameters
5. You want to generate button variants for 'primary', 'secondary', and 'danger' with colors blue, gray, and red using a Sass map and a mixin. Which code correctly creates all variants with minimal repetition?