Performance: Naming conventions
Naming conventions affect CSS selector performance and maintainability, impacting how quickly browsers match styles to elements.
Jump into concepts and practice - no test required
.nav-link:hover { color: red; }
.header .nav ul li a:hover { color: red; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deep descendant selectors (.header .nav ul li a:hover) | High (many elements checked) | 0 | Low | [X] Bad |
| Flat class selectors (.nav-link:hover) | Low (direct match) | 0 | Low | [OK] Good |
main-header. uses lowercase and hyphens, which is recommended. Options B and C use uppercase or camelCase, less common in CSS. Use spaces between words, like main header. uses spaces, which is invalid in class names.main-header. -> Option Cblock__element--modifier format. Double hyphens separate modifiers.--primary, matching BEM. Options A and B use camelCase or underscores, not BEM. button primary uses space, invalid in class names.card__title--large, what does it represent in BEM?card__title--large, card is the block, title is the element (after double underscore), and large is the modifier (after double hyphen).nav__item_active--, not underscores.nav__item_active uses a single underscore for the modifier, which is incorrect. It should be nav__item--active.card. Element is card__title. Modifier on block is card--highlighted.card, card__title, card--highlighted correctly uses BEM naming. card, card_title, card_highlighted uses underscores, not BEM. card, card-title, card-highlighted uses hyphens but not BEM format. card, card__title--highlighted, card__highlighted incorrectly applies modifier to element and duplicates modifier.card, card__title, card--highlighted -> Option A