Combining & with pseudo-classes in Sass
📖 Scenario: You are creating a simple button style for a website. The button should change its background color when hovered over and when it is focused (for keyboard users). You will use Sass nesting and combine the & symbol with pseudo-classes to style these states.
🎯 Goal: Build a Sass stylesheet that styles a button with a blue background and white text. When the button is hovered over, the background should become dark blue. When the button is focused, the background should become light blue. Use the & symbol combined with pseudo-classes :hover and :focus inside the button style.
📋 What You'll Learn
Create a Sass selector for a class named
.btn.Set the button's background color to
#007BFF and text color to white.Use the
&:hover pseudo-class to change the background color to #0056b3 when hovered.Use the
&:focus pseudo-class to change the background color to #66b2ff when focused.Use nesting and the
& symbol to combine the base selector with pseudo-classes.💡 Why This Matters
🌍 Real World
Buttons are everywhere on websites and apps. Styling them with hover and focus states improves user experience and accessibility.
💼 Career
Knowing how to use Sass nesting and combine & with pseudo-classes is a common skill for front-end developers to write clean and efficient stylesheets.
Progress0 / 4 steps