State classes help change how buttons or links look when you hover, click, or disable them. This makes websites easier to use and understand.
State class generation (hover, active, disabled) in SASS
Start learning this pattern below
Jump into concepts and practice - no test required
@mixin state-classes($base-class) { .#{$base-class} { &:hover { // hover styles here } &:active { // active styles here } &.disabled, &[disabled] { // disabled styles here pointer-events: none; opacity: 0.5; } } }
The @mixin helps create reusable style blocks.
Use & to refer to the current selector inside nested rules.
@mixin state-classes($base-class) { .#{$base-class} { &:hover { background-color: lightblue; } &:active { background-color: blue; color: white; } &.disabled, &[disabled] { opacity: 0.5; pointer-events: none; } } }
btn.@include state-classes('btn');
.btn { background-color: gray; color: black; padding: 0.5rem 1rem; border-radius: 0.25rem; cursor: pointer; }
This code creates a mixin to add hover, active, and disabled states to a button class. Then it includes the mixin for the btn class and sets basic button styles. The button changes color on hover and active, and looks faded and unclickable when disabled.
@mixin state-classes($base-class) { .#{$base-class} { &:hover { background-color: lightblue; } &:active { background-color: blue; color: white; } &.disabled, &[disabled] { opacity: 0.5; pointer-events: none; cursor: not-allowed; } } } @include state-classes('btn'); .btn { background-color: gray; color: black; padding: 0.5rem 1rem; border-radius: 0.25rem; cursor: pointer; user-select: none; transition: background-color 0.3s ease, color 0.3s ease, opacity 0.3s ease; }
Use pointer-events: none; on disabled states to prevent clicks.
Adding cursor: not-allowed; helps users see the button is disabled.
Transitions make state changes smooth and nicer to see.
State classes help show different looks for hover, active, and disabled states.
Use a Sass mixin to create these states easily for any class.
Disabled states should prevent clicks and show a faded look.
Practice
:hover, :active, and .disabled in Sass?Solution
Step 1: Understand state classes
State classes like:hoverand:activechange 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 AQuick Check:
State classes change look on interaction [OK]
- Thinking state classes create new elements
- Confusing state classes with animations
- Believing state classes load files
Solution
Step 1: Review Sass mixin syntax
Mixins use@mixin name { ... }and nested selectors use&:statefor pseudo-classes.Step 2: Check correct pseudo-class usage
Correct syntax uses&:hoverand&:activeinside the mixin.Final Answer:
@mixin states { &:hover { color: blue; } &:active { color: red; } } -> Option BQuick Check:
Use & with :hover and :active in mixins [OK]
- Omitting & before pseudo-classes
- Using class selectors instead of pseudo-classes
- Missing @mixin keyword
@mixin states {
&:hover { color: green; }
&:active { color: orange; }
&.disabled { color: gray; cursor: not-allowed; }
}
.button {
color: black;
@include states;
}Solution
Step 1: Understand the mixin states
The mixin setscolor: greenon:hover,color: orangeon:active, and gray with disabled class.Step 2: Check the hover state
When the button is hovered, the:hoverstyle applies, changing text color to green.Final Answer:
Green -> Option AQuick Check:
Hover changes color to green [OK]
- Confusing active color with hover color
- Ignoring the disabled class
- Assuming base color stays on hover
@mixin states {
&:hover { color: blue; }
&:active { color: red; }
&:disabled { color: gray; cursor: not-allowed; }
}
.button {
@include states;
}Solution
Step 1: Check pseudo-class vs class usage
:disabledis a pseudo-class for form elements, but here disabled is a class, so&.disabledis 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 CQuick Check:
Use .disabled class selector, not :disabled pseudo-class [OK]
- Confusing :disabled pseudo-class with .disabled class
- Forgetting semicolons
- Thinking mixins need capital letters
Solution
Step 1: Check disabled state styling
The disabled state should fade the button withopacity: 0.5and prevent clicks usingpointer-events: none.Step 2: Verify cursor and selector usage
Using&.disabledis correct for a class. Cursor should benot-allowedto show disabled status.Step 3: Compare options
@mixin button-states { &:hover { background-color: lightblue; } &:active { background-color: blue; } &.disabled { opacity: 0.5; pointer-events: none; cursor: not-allowed; } } correctly uses&.disabled, sets opacity to 0.5, disables pointer events, and sets cursor properly.Final Answer:
@mixin button-states { &:hover { background-color: lightblue; } &:active { background-color: blue; } &.disabled { opacity: 0.5; pointer-events: none; cursor: not-allowed; } } -> Option DQuick Check:
Disabled state fades and disables clicks with pointer-events none [OK]
- Using :disabled pseudo-class instead of .disabled class
- Not disabling pointer events on disabled
- Setting opacity to 1 in disabled state
