Performance: Parent selector with &
This affects CSS compilation time and the resulting CSS file size, which impacts page load speed.
.button { color: blue; &:hover { color: red; } }
.button { color: blue; } .button:hover { color: red; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual parent selector repetition | 0 | 0 | Low (larger CSS file) | [X] Bad |
| Using & parent selector in Sass | 0 | 0 | Low (smaller CSS file) | [OK] Good |
& symbol represent in Sass when used inside nested selectors?& in Sass nesting& symbol is used to refer back to the parent selector in nested Sass code.& does in the context& means parent selector [OK]&?& with a pseudo-class is &:hover without spaces or parentheses.&:hover correctly inside the button block.nav {
&.active {
color: blue;
}
ul & {
margin: 0;
}
}&.active usage& refers to nav, so &.active becomes nav.active.ul & usageul nav, because & is replaced by nav but placed after ul..card {
& .title {
font-weight: bold;
}
&.highlight {
background: yellow;
}
&hover {
color: red;
}
}& .title is valid for child selector, &.highlight is valid for combined class.&hover is missing the colon and should be &:hover.li elements inside a ul have a special style only when the ul has a class highlight. Which Sass code using & correctly achieves this?li inside ul.highlight, so the selector is ul.highlight li.&.highlight li inside ul, so & becomes ul, resulting in ul.highlight li.Pass the test and this pattern moves unknown