📖 Scenario: You are creating a simple webpage with a navigation menu. The menu has a main container and several links inside it. You want to style the menu and its links using Sass, keeping related styles grouped together for clarity and easier maintenance.
🎯 Goal: Build a Sass stylesheet that uses selector nesting to style a navigation menu container with a background color and padding, and style the links inside it with color and hover effects. The nested styles should be inside the main container style block.
📋 What You'll Learn
Create a Sass variable $nav-bg-color with the value #333.
Create a style block for a class .nav-menu with background color and padding.
Nest the styles for a links inside .nav-menu using selector nesting.
Inside the nested a styles, set the text color and add a hover effect that changes the color.
💡 Why This Matters
🌍 Real World
Web developers often use Sass nesting to keep related styles together, making CSS easier to read and maintain.
💼 Career
Knowing how to use Sass nesting is a valuable skill for frontend developers working on scalable and organized stylesheets.
Progress0 / 4 steps
1
Create the Sass variable for background color
Create a Sass variable called $nav-bg-color and set it to the color #333.
SASS
Hint
Use the $ symbol to create a variable in Sass.
2
Add styles for the navigation menu container
Create a style block for the class .nav-menu. Inside it, set background-color to $nav-bg-color and padding to 1rem.
SASS
Hint
Use .nav-menu { ... } to create the style block.
3
Nest styles for links inside the navigation menu
Inside the .nav-menu style block, nest a style block for a links. Set the color property to #fff.
SASS
Hint
Indent the a block inside .nav-menu to nest it.
4
Add hover effect to the nested links
Inside the nested a block, add a nested :hover style that changes the color to #ff0.
SASS
Hint
Use &:hover inside the a block to nest the hover style.
Practice
(1/5)
1. What is the main benefit of using property nesting in Sass?
easy
A. It groups related styles inside one block for better readability.
B. It automatically compiles CSS faster.
C. It removes the need for selectors in CSS.
D. It disables browser default styles.
Solution
Step 1: Understand the purpose of nesting
Nesting groups related CSS rules inside one block, making the code easier to read and maintain.
Step 2: Compare options
Only 'It groups related styles inside one block for better readability.' correctly describes this benefit. Other options describe unrelated or incorrect effects.
Final Answer:
It groups related styles inside one block for better readability. -> Option A
Quick Check:
Nesting groups styles = C [OK]
Hint: Nesting groups styles inside blocks for clarity [OK]
Common Mistakes:
Thinking nesting speeds up CSS compilation
Believing nesting removes selectors
Assuming nesting disables default styles
2. Which of the following is the correct Sass syntax to nest a :hover style inside a button class?
easy
A. .button:hover { & { color: red; } }
B. .button { &:hover { color: red; } }
C. &.button { :hover { color: red; } }
D. .button { :hover & { color: red; } }
Solution
Step 1: Identify correct nesting syntax
In Sass, & refers to the parent selector. To nest :hover, write .button { &:hover { ... } }.
Step 2: Check each option
.button { &:hover { color: red; } } uses correct syntax. Others misuse & or place pseudo-class incorrectly.
Final Answer:
.button { &:hover { color: red; } } -> Option B
Quick Check:
Nesting with & before :hover = A [OK]
Hint: Use & before pseudo-class inside parent block [OK]
5. You want to style a card component so that when hovered, its title text turns blue and the card background changes to light gray. Using property nesting, which Sass code achieves this correctly?