Angular SCSS BEM Standards
Overview
Use this skill to keep component styles predictable, reusable, and easy to review. Prefer component-scoped BEM classes and keep selectors flat by default.
Workflow
- •Identify the component selector and define one block name.
- •Map required elements and modifiers before editing template or style files.
- •Implement classes in template first, then write SCSS with flat selectors.
- •Validate against the checklist in
references/review-checklist.md. - •If selector context needs more than one descendant level, split into child components.
Component Structure & Scope
- •Root Class Naming: The root element of the component template MUST have a class that matches the component name (kebab-case).
- •Example:
LoginComponent-><div class="login ..."> - •Rationale: Facilitates debugging, testing, and strict BEM scoping.
- •Example:
- •Single Root Element: Wrap the entire component template in a single root element acting as the Block container.
- •Strict Block Inclusion: Global/Shared blocks (e.g.
.form) MUST explicitly wrap their elements (e.g..form__input).- •Invalid:
<input class="form__input">(Orphan element) - •Valid:
<div class="form"><input class="form__input"></div>
- •Invalid:
- •No Namespace Pollution: Do not use a specific component's class to style generic content in another component. Create a dedicated global block instead.
- •Invalid:
<div class="login public-shell__form-wrapper">(Login depends on Shell implementation) - •Valid:
<div class="login auth-layout">(Both use a generic.auth-layoutblock)
- •Invalid:
Core Rules
- •Use one BEM block per component and keep the block name stable.
- •Prefer SCSS Nesting with Parent Selector (
&):- •Use
&__elementand&--modifierto keep the Block name DRY. - •Example:
.card { &__title { ... } }compiles to.card__title.
- •Use
- •Compiled CSS must be flat: The goal is single-class specificity in the output CSS, not necessarily flat SCSS source code.
- •Keep nesting depth to two levels in naming; do not create
block__element__sub. - •Contextual Overrides: Use parent selectors for context-specific overrides within the same block scope.
- •Valid:
&__header &__logo { ... }(Target&__logoonly when inside&__header) - •This allows styling components based on their placement without deep nesting.
- •Valid:
- •Do not chain descendants beyond one level (for example
.block__a .block__b .block__cis invalid). - •Allow
>,+,~when used within the same block context and kept to one level. - •Avoid tag-qualified selectors and id selectors for component styling.
- •Use modifiers for variants and states; keep base class present at the same time.
- •Use global design tokens (for example
var(--space-*),var(--color-*)) instead of hard-coded values. - •Keep component styles in
*.component.scss; do not move feature-specific styles to global files.
Angular Integration Rules
- •Bind modifiers with class bindings, for example
[class.card--active]="isActive()". - •Keep template class names semantic and aligned with BEM map.
- •Prefer modifier classes first; use one-level descendant selectors or contextual overrides only when necessary.
- •Use Angular native control flow (
@if,@for,@switch) without changing class semantics.
Contextual Overrides Pattern
Use this pattern to style an element differently based on its container within the same Block, avoiding new modifier classes for every positional tweak.
Valid:
scss
.card {
&__header {
display: flex;
align-items: center;
}
// Contextual Override: Logo inside Header
&__header &__logo {
margin-right: var(--space-4);
height: 32px;
}
// Standard Element
&__logo {
height: 48px;
}
}
Invalid (Deep Nesting):
scss
.card {
&__header {
// Bad: deeply nested selector
.card__logo {
...
}
}
}
Output Contract
When implementing or refactoring, produce:
- •A short class map (
block,elements,modifiers). - •Updated template snippets with BEM classes.
- •Updated SCSS snippets using flat BEM selectors.
- •A checklist result that marks pass/fail items from
references/review-checklist.md.
References
Use these references when needed:
- •
references/review-checklist.mdfor review criteria and fix patterns.