Nesting selectors helps you write CSS in a cleaner and more organized way. It groups related styles together, making your code easier to read and maintain.
Basic selector nesting in SASS
& { nested-selector { property: value; } }The & symbol represents the parent selector.
You can nest selectors inside each other to reflect HTML structure.
title inside an element with class card..card { .title { color: blue; } }
ul inside a nav element.nav {
ul {
list-style: none;
}
}button..button { &:hover { background-color: green; } }
This example shows a card with a title and description. The styles use nested selectors in SASS to group styles for .title and .description inside .card. The card also changes background color on hover. The tabindex="0" makes the card focusable by keyboard for accessibility.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Basic Selector Nesting Example</title> <style> /* Compiled CSS from SASS nesting example */ .card { border: 2px solid #333; padding: 1rem; max-width: 300px; font-family: Arial, sans-serif; } .card .title { color: #2a7ae2; font-weight: bold; font-size: 1.25rem; margin-bottom: 0.5rem; } .card .description { color: #555; font-size: 1rem; } .card:hover { background-color: #f0f8ff; } </style> </head> <body> <section class="card" tabindex="0" aria-label="Information card"> <h2 class="title">Welcome to Nesting</h2> <p class="description">This card uses nested selectors in SASS for styling.</p> </section> </body> </html>
Remember that nesting too deeply can make your CSS hard to read and maintain.
Use the & symbol to refer to the parent selector when needed, like for pseudo-classes.
Always test your compiled CSS in the browser to see the visual effect.
Nesting groups related CSS selectors inside a parent for cleaner code.
The & symbol represents the parent selector in nested rules.
Use nesting to reflect the HTML structure and avoid repeating selectors.
Practice
& symbol represent in Sass nesting?Solution
Step 1: Understand the role of
The&in Sass&symbol is used inside nested selectors to refer to the parent selector.Step 2: Apply this understanding to nesting
When you nest selectors,&helps avoid repeating the parent selector by representing it directly.Final Answer:
The parent selector in the nested rule -> Option AQuick Check:
&means parent selector [OK]
- Thinking & creates a new class
- Confusing & with CSS properties
- Assuming & is a comment
.button class inside a .container class?Solution
Step 1: Identify correct nesting syntax in Sass
In Sass, nesting is done by placing the child selector inside the parent block using curly braces.Step 2: Check each option
.container { .button { color: blue; } } correctly nests.buttoninside.containerusing braces. Options B and C are plain CSS selectors, not Sass nesting. .container, .button { color: blue; } combines selectors incorrectly.Final Answer:
.container { .button { color: blue; } } -> Option CQuick Check:
Nested selectors use braces inside parent [OK]
- Using plain CSS selectors instead of nesting
- Combining selectors with commas incorrectly
- Missing curly braces for nesting
.nav {
ul {
margin: 0;
li {
list-style: none;
}
}
}What is the compiled CSS output?
Solution
Step 1: Understand nesting levels
Theulis nested inside.nav, so the selector becomes.nav ul. Theliis nested insideul, so it becomes.nav ul li.Step 2: Write the compiled CSS
Each nested block compiles to a combined selector with all parents. Properties stay inside their respective selectors.Final Answer:
.nav ul { margin: 0; } .nav ul li { list-style: none; } -> Option DQuick Check:
Nesting combines selectors in order [OK]
- Leaving nested selectors inside braces in output
- Combining properties incorrectly
- Using commas instead of nesting
.card {
&.active {
background: green;
}
.title {
font-weight: bold;
}
& .subtitle {
font-size: 1.2rem;
}
}Solution
Step 1: Analyze the
The space after& .subtitleselector&means it selects.card .subtitle. This is different from&.subtitlewhich selects.card.subtitle.Step 2: Check other parts for errors
Semicolons are present,&usage with classes is valid, and nesting.titleinside.cardis correct.Final Answer:
The selector& .subtitleadds an unwanted space, changing meaning. -> Option BQuick Check:
Space after & changes selector meaning [OK]
- Adding space after & when not intended
- Thinking & cannot be combined with classes
- Missing semicolons (but they are present here)
.card only when the card is .active. Which Sass nesting produces the correct CSS selector .card.active button?Solution
Step 1: Understand the target selector
The selector.card.active buttonmeans abuttoninside a.cardthat also has the.activeclass.Step 2: Check nesting structure
.card { &.active { button { color: red; } } } nestsbuttoninside&.activeinside.card, producing.card.active button. Other options either place.activeon the button or use incorrect nesting.Final Answer:
.card { &.active { button { color: red; } } } -> Option AQuick Check:
Nesting &.active then button matches .card.active button [OK]
- Placing .active on button instead of card
- Using & with space incorrectly
- Not nesting button inside &.active
Ready to prove it?
Pass the test and this pattern moves unknown
