Component variant generation helps you create different styles of the same element easily. It saves time and keeps your code organized.
Component variant generation in SASS
Start learning this pattern below
Jump into concepts and practice - no test required
@mixin component-variant($name) { &--#{$name} { @content; } } .component { // base styles @include component-variant('variant-name') { // variant styles } }
The @mixin defines reusable style blocks.
The &--#{$name} creates a modifier class like .component--variant-name.
.button--red and .button--blue with different background colors.@mixin button-variant($color) { &--#{$color} { background-color: $color; color: white; } } .button { padding: 1rem 2rem; border-radius: 0.5rem; font-weight: bold; @include button-variant(red); @include button-variant(blue); }
@mixin alert-variant($bg-color, $text-color) { &--custom { background-color: $bg-color; color: $text-color; padding: 1rem; border-radius: 0.25rem; } } .alert { font-size: 1rem; font-weight: 600; @include alert-variant(green, white); }
This example shows two buttons with different colors using component variant generation in Sass. The base .button class has common styles. The variants .button--red and .button--blue add different background colors. Hover effects darken the colors for better interaction feedback.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Button Variants</title> <style type="text/scss"> $red: #e63946; $blue: #457b9d; @mixin button-variant($color) { &--#{$color} { background-color: $color; color: white; border: none; cursor: pointer; @content; } } .button { padding: 1rem 2rem; border-radius: 0.5rem; font-weight: bold; font-family: Arial, sans-serif; margin: 0.5rem; transition: background-color 0.3s ease; @include button-variant(red) { background-color: $red; } @include button-variant(blue) { background-color: $blue; } } .button--red:hover { background-color: darken($red, 10%); } .button--blue:hover { background-color: darken($blue, 10%); } </style> </head> <body> <button class="button button--red" aria-label="Red button">Red Button</button> <button class="button button--blue" aria-label="Blue button">Blue Button</button> </body> </html>
Use meaningful variant names to keep your code easy to understand.
Component variants help keep your CSS DRY (Don't Repeat Yourself).
Always test your variants on different screen sizes for good responsiveness.
Component variant generation creates different styles for the same element easily.
Use Sass mixins with dynamic class names for flexible variants.
Variants keep your styles organized and reusable.
Practice
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 AQuick Check:
Component variant generation = multiple style versions [OK]
- Confusing Sass with JavaScript
- Thinking Sass compiles to JS
- Believing variants remove unused CSS
Solution
Step 1: Identify mixin syntax
Mixins use '@mixin' to define reusable style blocks with parameters.Step 2: Check options
@mixin button-variant($color) { background-color: $color; } uses '@mixin' correctly; others misuse '@function', '@include', or '@extend'.Final Answer:
@mixin button-variant($color) { background-color: $color; } -> Option DQuick Check:
Mixin definition starts with '@mixin' [OK]
- Using '@function' instead of '@mixin'
- Trying to define styles inside '@include'
- Confusing '@extend' with mixin definition
@mixin variant($name, $color) {
.btn-#{$name} {
background-color: $color;
}
}
@include variant('primary', blue);
@include variant('danger', red);What CSS will this generate?
Solution
Step 1: Understand interpolation in class names
The '#{$name}' inside '.btn-#{$name}' inserts the string value of $name, creating '.btn-primary' and '.btn-danger'.Step 2: Check property values
The background-color uses the passed $color values 'blue' and 'red' correctly.Final Answer:
.btn-primary { background-color: blue; } .btn-danger { background-color: red; } -> Option AQuick Check:
Interpolation creates correct class names [OK]
- Leaving interpolation as literal text
- Confusing background-color with color property
- Not passing parameters correctly
@mixin variant($name, $color) {
.btn-$name {
background-color: $color;
}
}
@include variant('success', green);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 CQuick Check:
Use '#{}' to insert variables in selectors [OK]
- Forgetting interpolation syntax
- Thinking variables can't be in selectors
- Misusing mixin parameters
Solution
Step 1: Understand map usage with @each
Using '@each $name, $color in $map' loops over keys and values, perfect for named variants.Step 2: Check each option's approach
@mixin variants($map) { @each $name, $color in $map { .btn-#{$name} { background-color: $color; } } } $btn-colors: (primary: blue, secondary: gray, danger: red); @include variants($btn-colors); correctly loops over a map with names and colors, generating '.btn-primary', '.btn-secondary', '.btn-danger' with correct colors.Step 3: Identify issues in other options
@mixin variants($map) { .btn { background-color: map-get($map, primary); } } $btn-colors: (primary: blue, secondary: gray, danger: red); @include variants($btn-colors); only styles '.btn' once, ignoring variants. @mixin variants($map) { @for $i from 1 through length($map) { .btn-#{$i} { background-color: nth($map, $i); } } } $btn-colors: (blue, gray, red); @include variants($btn-colors); uses numeric indexes without names. @mixin variants($map) { @each $color in $map { .btn-#{$color} { background-color: $color; } } } $btn-colors: (blue, gray, red); @include variants($btn-colors); loops colors but uses color names as class names incorrectly.Final Answer:
@mixin variants($map) { @each $name, $color in $map { .btn-#{$name} { background-color: $color; } } } $btn-colors: (primary: blue, secondary: gray, danger: red); @include variants($btn-colors); -> Option BQuick Check:
Use @each with map keys and values for variants [OK]
- Looping only colors without names
- Using numeric loops without keys
- Not generating separate classes per variant
