Performance: BEM naming with SASS nesting
This affects CSS parsing and rendering speed by influencing selector complexity and style recalculation.
Jump into concepts and practice - no test required
.block { &__element--modifier { color: red; } } // Flattened nesting with combined BEM class
.block { &__element { &--modifier { color: red; } } } // Deep nesting with many levels
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deep nested BEM selectors | No extra DOM nodes | Multiple reflows if styles change | Higher paint cost due to complex styles | [X] Bad |
| Flat BEM combined classes | No extra DOM nodes | Single reflow on style change | Lower paint cost with simpler styles | [OK] Good |
__ double underscore represent in BEM naming?__ connects a block to its element, e.g., block__element.block--active inside .block?& represents the parent selector inside nesting.block--active, use &--active inside .block nesting..card {
&__title {
font-weight: bold;
}
}& represents the parent selector .card..card and __title forms .card__title..menu {
&__item {
color: blue;
}
&--active {
color: red;
}
&__item--active {
font-weight: bold;
}
&-extra {
padding: 10px;
}
}&__item, &--active, and &__item--active follow BEM rules correctly.&-extra&-extra is invalid because BEM modifiers use double hyphen --, not single hyphen..form with an element __input and a modifier --error on the element. Which SASS nesting code correctly applies red border only when the input has the error modifier?__input is nested inside .form using &__input.--error applies to the element, so inside &__input nest &--error..form__input--error selector with red border, applying only when input has error modifier.