📖 Scenario: You are styling a simple webpage with a navigation menu and some content sections. You want to write clean and easy-to-read Sass code without nesting selectors too deeply.
🎯 Goal: Write Sass code that styles a navigation bar and content sections using nesting but avoid over-nesting beyond two levels to keep the code simple and maintainable.
📋 What You'll Learn
Create a Sass variable for the main color
Use nesting up to two levels only
Style the navigation bar background and links
Style the content section background and headings
Avoid nesting selectors more than two levels deep
💡 Why This Matters
🌍 Real World
Writing clean and maintainable CSS using Sass is important for real websites to keep styles organized and easy to update.
💼 Career
Front-end developers often use Sass to write scalable stylesheets. Avoiding over-nesting is a best practice that improves code quality and teamwork.
Progress0 / 4 steps
1
Create a Sass variable for the main color
Create a Sass variable called $main-color and set it to #3498db.
SASS
Hint
Use the $ symbol to create a variable in Sass.
2
Style the navigation bar background and links with nesting
Write Sass code to style a nav element with a background color of $main-color. Inside nav, style a links to have white text color and no underline. Use nesting up to two levels only.
SASS
Hint
Use nesting inside nav to style a links.
3
Style the content section background and headings with nesting
Write Sass code to style a .content class with a light gray background color #f0f0f0. Inside .content, style h2 headings to have the color $main-color. Use nesting up to two levels only.
SASS
Hint
Use nesting inside .content to style h2 headings.
4
Avoid over-nesting by not nesting more than two levels
Ensure your Sass code does not nest selectors more than two levels deep anywhere. For example, do not nest ul inside nav inside another selector. Add a style for ul inside nav with padding: 0; using only two levels of nesting.
SASS
Hint
Keep nesting inside nav only two levels deep by adding ul alongside a.
Practice
(1/5)
1. Why is it recommended to avoid nesting more than 2 levels deep in Sass?
easy
A. Because Sass does not support nesting beyond 2 levels
B. Because deep nesting makes CSS harder to read and slower to load
C. Because deep nesting automatically breaks the styles
D. Because browsers ignore styles nested more than 2 levels
Solution
Step 1: Understand the impact of deep nesting
Deep nesting creates very specific selectors that are hard to read and maintain.
Step 2: Consider browser performance
Browsers take longer to match deeply nested selectors, slowing page rendering.
Final Answer:
Because deep nesting makes CSS harder to read and slower to load -> Option B
Quick Check:
Deep nesting = Hard to read and slow [OK]
Hint: Keep nesting shallow to improve readability and speed [OK]
Common Mistakes:
Thinking Sass limits nesting levels
Believing browsers ignore deep nesting
Assuming deep nesting breaks styles
2. Which of the following Sass snippets correctly avoids over-nesting?
Deep nesting like in .section { .card { .button { background: green; } } } is discouraged; flat or shallow nesting is better.
Step 2: Analyze .section { .card { &-button { background: green; } } }'s use of &-button
.section { .card { &-button { background: green; } } } nests only 2 levels and uses the parent selector & to create .card-button, keeping nesting shallow and naming clear.
Step 3: Compare with other options
.section-card-button { background: green; } flattens all into one class, losing semantic structure. .section { .card-button { background: green; } } nests only 1 level but creates a combined class that may not reflect HTML structure well.