Performance: Selector nesting for related styles
This affects CSS parsing and rendering speed by influencing selector complexity and style recalculation.
nav ul li a {
color: blue;
}nav {
ul {
li {
a {
color: blue;
}
}
}
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deeply nested selectors | No extra DOM nodes | 0 | Higher due to complex style matching | [X] Bad |
| Flat selectors | No extra DOM nodes | 0 | Lower due to simpler style matching | [OK] Good |
:hover style inside a button class?& refers to the parent selector. To nest :hover, write .button { &:hover { ... } }.& or place pseudo-class incorrectly..card {
background: white;
&:hover {
background: gray;
}
.title {
font-weight: bold;
}
}&:hover becomes .card:hover. The nested .title becomes .card .title..nav {
color: black;
&:hover {
color: blue;
}
&.active {
color: red;
}
.item {
font-size: 1rem;
}
&:item {
font-weight: bold;
}
}&:hover and &.active are valid. .item nested inside .nav is correct.&:item is invalid because :item is not a valid pseudo-class. It should be .item without &:.&:hover .title to style the title when the card is hovered, which is correct. .card {
background: white;
&:hover {
.title {
background: lightgray;
color: blue;
}
}
.title {
color: black;
}
} nests .title inside &:hover but incorrectly applies background: lightgray to .title instead of the card. .card {
background: white;
.title {
color: black;
&:hover {
color: blue;
}
}
&:hover {
background: lightgray;
}
} incorrectly applies hover on .title itself. .card {
background: white;
&:hover {
background: lightgray;
}
.title:hover {
color: blue;
}
.title {
color: black;
}
} styles .title:hover, which triggers only when hovering the title, not the card.Pass the test and this pattern moves unknown