Discover how a single symbol can save you from endless copying and pasting in your styles!
Why Parent selector with & in SASS? - Purpose & Use Cases
Imagine you want to style a button and its hover state. You write separate CSS rules for .button and .button:hover manually.
Writing separate selectors means repeating the parent class name each time. If you rename the parent class, you must update every related selector manually, which is slow and error-prone.
The parent selector & in Sass lets you nest styles and refer to the parent selector automatically. This keeps your code DRY and easier to maintain.
.button { color: blue; }
.button:hover { color: red; }.button {
color: blue;
&:hover {
color: red;
}
}You can write cleaner, nested styles that automatically link to the parent selector, making your CSS easier to read and update.
When styling a navigation menu, you can nest hover and active states inside the main menu item style using &, so all related styles stay grouped and consistent.
Manually repeating selectors is tedious and error-prone.
The & parent selector lets you nest and reuse the parent selector easily.
This leads to cleaner, maintainable, and DRY CSS code.
Practice
& symbol represent in Sass when used inside nested selectors?Solution
Step 1: Understand the role of
The&in Sass nesting&symbol is used to refer back to the parent selector in nested Sass code.Step 2: Identify what
It helps avoid repeating the parent selector by representing it directly.&does in the contextFinal Answer:
It refers to the current parent selector. -> Option CQuick Check:
&means parent selector [OK]
- Thinking & creates a new selector
- Confusing & with a comment symbol
- Assuming & resets styles
&?Solution
Step 1: Check the correct pseudo-class syntax with &
The correct way to use&with a pseudo-class is&:hoverwithout spaces or parentheses.Step 2: Identify the valid Sass nesting syntax
button { &:hover { color: red; } } uses&:hovercorrectly inside the button block.Final Answer:
button { &:hover { color: red; } } -> Option DQuick Check:
Use &:pseudo-class with no spaces [OK]
- Adding parentheses after pseudo-class
- Leaving space between & and pseudo-class
- Writing &hover instead of &:hover
nav {
&.active {
color: blue;
}
ul & {
margin: 0;
}
}Which CSS selectors will be generated?
Solution
Step 1: Understand
The&.activeusage&refers tonav, so&.activebecomesnav.active.Step 2: Understand
This meansul &usageul nav, because&is replaced bynavbut placed afterul.Final Answer:
nav.active { color: blue; } and ul nav { margin: 0; } -> Option AQuick Check:
& replaces parent selector exactly [OK]
- Assuming & always prefixes the selector
- Confusing space placement around &
- Treating & as a separate selector
.card {
& .title {
font-weight: bold;
}
&.highlight {
background: yellow;
}
&hover {
color: red;
}
}Solution
Step 1: Check each & usage
& .titleis valid for child selector,&.highlightis valid for combined class.Step 2: Identify incorrect pseudo-class syntax
&hoveris missing the colon and should be&:hover.Final Answer:
Missing colon in &:hover pseudo-class -> Option AQuick Check:
Pseudo-classes need colon after & [OK]
- Forgetting colon before pseudo-class
- Thinking space after & is always wrong
- Assuming & can't combine with classes
li elements inside a ul have a special style only when the ul has a class highlight. Which Sass code using & correctly achieves this?Solution
Step 1: Understand the selector goal
We want to styleliinsideul.highlight, so the selector isul.highlight li.Step 2: Check which Sass code produces that selector
ul { &.highlight li { color: green; } } nests&.highlight liinsideul, so&becomesul, resulting inul.highlight li.Final Answer:
ul { &.highlight li { color: green; } } -> Option BQuick Check:
& replaces parent selector exactly where placed [OK]
- Misplacing & causing wrong selector order
- Trying to put & inside child selector
- Confusing class placement on li instead of ul
Ready to prove it?
Pass the test and this pattern moves unknown
