📖 Scenario: You are creating a simple webpage with a button. You want to style the button and also style the button when it is hovered over. To keep your styles organized, you will use Sass and the parent selector & to write nested styles.
🎯 Goal: Build a Sass stylesheet that styles a button with a blue background and white text. When the user hovers over the button, the background should change to dark blue. Use the parent selector & to nest the hover style inside the button style.
📋 What You'll Learn
Create a Sass class called .btn with a blue background and white text color.
Add a nested hover style inside .btn using the parent selector &.
The hover style changes the background color to dark blue.
Use proper nesting and the & selector in Sass.
💡 Why This Matters
🌍 Real World
Buttons are common on websites and apps. Using Sass nesting with & helps keep styles organized and maintainable.
💼 Career
Front-end developers often use Sass to write cleaner CSS. Knowing how to use & for nesting is a key skill for styling interactive elements.
Progress0 / 4 steps
1
Create the base button style
Create a Sass class called .btn that sets the background color to #007BFF (blue) and the text color to #FFFFFF (white).
SASS
Hint
Use background-color and color properties inside the .btn class.
2
Add the hover state configuration
Add a nested style inside the .btn class using the parent selector & to target the hover state with :hover. Just add the selector line &:hover inside .btn.
SASS
Hint
Use &:hover inside .btn to start the hover style.
3
Set the hover background color
Inside the nested &:hover selector, set the background color to #0056b3 (dark blue).
SASS
Hint
Inside &:hover, write background-color: #0056b3;.
4
Complete the Sass with proper nesting
Make sure the entire Sass code has the .btn class with nested &:hover selector and the correct background colors and text color as specified.
SASS
Hint
Review your code to ensure all parts are nested correctly and colors match.
Practice
(1/5)
1. What does the & symbol represent in Sass when used inside nested selectors?
easy
A. It resets all styles to default.
B. It creates a new global selector.
C. It refers to the current parent selector.
D. It comments out the nested block.
Solution
Step 1: Understand the role of & in Sass nesting
The & symbol is used to refer back to the parent selector in nested Sass code.
Step 2: Identify what & does in the context
It helps avoid repeating the parent selector by representing it directly.
Final Answer:
It refers to the current parent selector. -> Option C
Quick Check:
& means parent selector [OK]
Hint: Remember & means "this parent" selector in Sass [OK]
Common Mistakes:
Thinking & creates a new selector
Confusing & with a comment symbol
Assuming & resets styles
2. Which of the following is the correct Sass syntax to style a button with a hover effect using the parent selector &?
easy
A. button {
&hover {
color: red;
}
}
B. button {
& :hover {
color: red;
}
}
C. button {
&:hover() {
color: red;
}
}
D. button {
&:hover {
color: red;
}
}
Solution
Step 1: Check the correct pseudo-class syntax with &
The correct way to use & with a pseudo-class is &:hover without spaces or parentheses.
& .title is valid for child selector, &.highlight is valid for combined class.
Step 2: Identify incorrect pseudo-class syntax
&hover is missing the colon and should be &:hover.
Final Answer:
Missing colon in &:hover pseudo-class -> Option A
Quick Check:
Pseudo-classes need colon after & [OK]
Hint: Pseudo-classes need &: not & alone [OK]
Common Mistakes:
Forgetting colon before pseudo-class
Thinking space after & is always wrong
Assuming & can't combine with classes
5. You want to style a list where the li elements inside a ul have a special style only when the ul has a class highlight. Which Sass code using & correctly achieves this?
hard
A. ul.highlight {
li & {
color: green;
}
}
B. ul {
&.highlight li {
color: green;
}
}
C. ul {
li {
&.highlight {
color: green;
}
}
}
D. ul {
& li.highlight {
color: green;
}
}
Solution
Step 1: Understand the selector goal
We want to style li inside ul.highlight, so the selector is ul.highlight li.
Step 2: Check which Sass code produces that selector
ul {
&.highlight li {
color: green;
}
} nests &.highlight li inside ul, so & becomes ul, resulting in ul.highlight li.
Final Answer:
ul { &.highlight li { color: green; } } -> Option B
Quick Check:
& replaces parent selector exactly where placed [OK]
Hint: Place & where parent selector should appear in nested code [OK]
Common Mistakes:
Misplacing & causing wrong selector order
Trying to put & inside child selector
Confusing class placement on li instead of ul
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.