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
State Class Generation with SASS
📖 Scenario: You are creating a button style in SASS that changes appearance based on user interaction states like hover, active, and disabled. This helps users understand when a button is clickable, pressed, or not available.
🎯 Goal: Build a SASS style that defines a base button class and generates styles for :hover, :active, and .disabled states using SASS features.
📋 What You'll Learn
Create a SASS map named $states with keys hover, active, and disabled and their respective color values.
Create a variable $base-color with the value #3498db.
Use a @each loop to generate CSS for each state in $states inside the .button class.
Add the .disabled class style with cursor: not-allowed and opacity: 0.6.
💡 Why This Matters
🌍 Real World
Buttons on websites often change appearance when hovered, clicked, or disabled to give users clear feedback.
💼 Career
Knowing how to write maintainable and reusable styles with SASS is valuable for front-end developers working on interactive UI components.
Progress0 / 4 steps
1
Create the base color and states map
Create a SASS variable called $base-color and set it to #3498db. Then create a SASS map called $states with these exact entries: hover: #2980b9, active: #1c5980, and disabled: #95a5a6.
SASS
Hint
Use $base-color: #3498db; and $states: (hover: #2980b9, active: #1c5980, disabled: #95a5a6);
2
Start the button base style
Create a CSS class called .button and set its background-color to the variable $base-color. Also add color: white and padding: 1rem 2rem inside the .button block.
Inside the .button class, use a @each loop with variables $state and $color to loop over $states. For each $state except disabled, create a nested selector using &:#{$state} and set background-color to $color. Use an @if statement to skip the disabled state inside the loop.
SASS
Hint
Use @each $state, $color in $states and inside it an @if $state != disabled to create &:#{$state} with background-color: $color;
4
Add disabled state style
Outside the @each loop but still inside the .button class, add a nested selector for .disabled that sets background-color to map-get($states, disabled), cursor to not-allowed, and opacity to 0.6.
SASS
Hint
Write &.disabled with background-color: map-get($states, disabled);, cursor: not-allowed;, and opacity: 0.6;
Practice
(1/5)
1. What is the main purpose of using state classes like :hover, :active, and .disabled in Sass?
easy
A. To change the appearance of elements based on user interaction or status
B. To add animations to elements automatically
C. To create new HTML elements dynamically
D. To load external CSS files conditionally
Solution
Step 1: Understand state classes
State classes like :hover and :active change how elements look when users interact with them.
Step 2: Identify their purpose
They help show different styles for hover, active, or disabled states to improve user experience.
Final Answer:
To change the appearance of elements based on user interaction or status -> Option A
Quick Check:
State classes change look on interaction [OK]
Hint: State classes show style changes on user actions [OK]
Common Mistakes:
Thinking state classes create new elements
Confusing state classes with animations
Believing state classes load files
2. Which of the following is the correct Sass syntax to create a mixin for hover and active states?
easy
A. @mixin states { :hover { color: blue; } :active { color: red; } }
B. @mixin states { &:hover { color: blue; } &:active { color: red; } }
C. @mixin states { .hover { color: blue; } .active { color: red; } }
D. @mixin states { hover { color: blue; } active { color: red; } }
Solution
Step 1: Review Sass mixin syntax
Mixins use @mixin name { ... } and nested selectors use &:state for pseudo-classes.
Step 2: Check correct pseudo-class usage
Correct syntax uses &:hover and &:active inside the mixin.
Final Answer:
@mixin states { &:hover { color: blue; } &:active { color: red; } } -> Option B
Quick Check:
Use & with :hover and :active in mixins [OK]
Hint: Use & before pseudo-classes inside mixins [OK]
Common Mistakes:
Omitting & before pseudo-classes
Using class selectors instead of pseudo-classes
Missing @mixin keyword
3. Given this Sass code, what color will the button text be when hovered?
C. Using &:disabled instead of &.disabled for disabled class
D. Mixin name should be capitalized
Solution
Step 1: Check pseudo-class vs class usage
:disabled is a pseudo-class for form elements, but here disabled is a class, so &.disabled is correct.
Step 2: Verify other syntax
Semicolons are present, mixin naming is flexible, and mixins can be used inside classes.
Final Answer:
Using &:disabled instead of &.disabled for disabled class -> Option C
Quick Check:
Use .disabled class selector, not :disabled pseudo-class [OK]
Hint: Use .disabled class, not :disabled pseudo-class [OK]
Common Mistakes:
Confusing :disabled pseudo-class with .disabled class
Forgetting semicolons
Thinking mixins need capital letters
5. You want to create a reusable Sass mixin that adds hover, active, and disabled states to any button. The disabled state should make the button look faded and prevent clicks. Which of these mixins correctly implements this behavior?