SASS Nesting and Selectors 1 — Questions and Answers
Question 1: What does the `&` symbol represent in Sass nesting?
- The parent selector (Correct answer)
- A universal selector
- An ID selector
- A child combinator
Correct answer: The parent selector
In Sass, `&` refers to the parent selector and is used to create compound selectors or pseudo-classes.
Question 2: What is the compiled CSS output of `.button { &:hover { color: red; } }`?
- .button:hover { color: red; } (Correct answer)
- .button .hover { color: red; }
- .button > :hover { color: red; }
- button:hover { color: red; }
Correct answer: .button:hover { color: red; }
Sass replaces `&` with the parent selector, producing `.button:hover` in the CSS output.
Question 3: How deep should Sass nesting ideally go to maintain readable CSS?
- No more than 3 levels (Correct answer)
- As deep as needed
- No more than 10 levels
- Exactly 5 levels
Correct answer: No more than 3 levels
Best practice recommends limiting Sass nesting to 3 levels to avoid specificity issues and unreadable output.
Question 4: Which Sass feature allows you to nest `@media` queries inside selectors?
- Nested `@media` directives (Correct answer)
- Inline media queries
- Responsive mixins
- `@include media`
Correct answer: Nested `@media` directives
Sass supports nesting `@media` rules inside selectors, which are bubbled up to the top level in output.
Question 5: What does the following Sass produce: `.parent { .child { color: blue; } }`?
- .parent .child { color: blue; } (Correct answer)
- .parent > .child { color: blue; }
- .parent.child { color: blue; }
- .child { color: blue; }
Correct answer: .parent .child { color: blue; }
Sass nesting compiles nested selectors into a descendant combinator in the CSS output.
Question 6: How do you use `&` to create a BEM modifier in Sass?
- .block { &__element { } &--modifier { } } (Correct answer)
- .block { #element { } #modifier { } }
- .block { .element { } .modifier { } }
- .block { *element { } *modifier { } }
Correct answer: .block { &__element { } &--modifier { } }
Using `&__element` and `&--modifier` with the parent selector allows clean BEM notation in Sass.
What does the `&` symbol represent in Sass nesting?