home / skills / develite98 / angular-best-practices / angular-css-bem-best-practices

angular-css-bem-best-practices skill

/skills/angular-css-bem-best-practices

This skill helps you apply Angular with BEM best practices to create reusable, well-structured component styles and enforce consistent naming.

npx playbooks add skill develite98/angular-best-practices --skill angular-css-bem-best-practices

Review the files below or copy the command above to add this skill to your agents.

Files (9)
SKILL.md
4.0 KB
---
name: angular-css-bem-best-practices
description: Angular + BEM CSS methodology guide for creating reusable components and shareable front-end code. Enforces component-scoped BEM blocks, max 2-level nesting, proper component decomposition, semantic element naming, correct modifier patterns, and flat selectors. Use when writing, reviewing, or refactoring Angular component styles. Triggers on tasks involving CSS, SCSS, SASS, component styling, BEM naming, or CSS architecture.
license: MIT
metadata:
  author: community
  version: "1.0.0"
---

# Angular + BEM CSS Best Practices

A methodology guide for combining Angular's component architecture with BEM (Block Element Modifier) CSS naming convention to create reusable components and enable code sharing in front-end development. Contains 6 rules with bad/good examples in CSS, SCSS, and SASS.

## When to Apply

Reference these guidelines when:
- Writing CSS/SCSS/SASS for Angular components
- Naming CSS classes in Angular templates
- Reviewing component styles for consistency
- Deciding whether to split a component based on styling complexity
- Setting up CSS architecture for a new Angular project
- Refactoring existing styles to follow BEM methodology

## Core Principles

- **1 Component = 1 BEM Block** — The block name matches the component selector
- **Max 2 Levels** — Only `Block` and `Block__Element`, never `Block__Element__SubElement`
- **Split When Deep** — If you need a third level, extract a child component
- **Flat Selectors** — No descendant, child, or tag-qualified selectors
- **Semantic Names** — Element names describe _what_, not _how_ or _where_
- **Modifiers for Variants** — Use `--modifier` for states and variants, always with the base class

## Rule Categories by Priority

| Priority | Rule | Impact | File |
|----------|------|--------|------|
| 1 | Block = Component Selector | CRITICAL | `bem-block-selector` |
| 2 | Max 2 Levels of Nesting | CRITICAL | `bem-max-nesting` |
| 3 | Split Child Components | CRITICAL | `bem-split-components` |
| 4 | Element Naming Conventions | HIGH | `bem-element-naming` |
| 5 | Modifier Patterns | HIGH | `bem-modifier-patterns` |
| 6 | No Cascading Selectors | HIGH | `bem-no-cascading` |

## Quick Reference

### 1. Block = Component Selector (CRITICAL)

- `bem-block-selector` - BEM block name must match the Angular component selector (minus prefix)

### 2. Maximum 2 Levels (CRITICAL)

- `bem-max-nesting` - Never nest beyond `.block__element` — no `.block__element__subelement`

### 3. Split Child Components (CRITICAL)

- `bem-split-components` - Extract child components when BEM depth would exceed 2 levels

### 4. Element Naming (HIGH)

- `bem-element-naming` - Use semantic, descriptive kebab-case names for BEM elements

### 5. Modifier Patterns (HIGH)

- `bem-modifier-patterns` - Use `--modifier` correctly for states and variants with Angular class bindings

### 6. No Cascading (HIGH)

- `bem-no-cascading` - Avoid descendant, child, and tag-qualified selectors — keep BEM flat

## BEM Cheat Sheet

```
.block                    → Component root (matches selector)
.block__element           → Child part of the component
.block--modifier          → Variant of the entire block
.block__element--modifier → Variant of a single element

✅ .user-card
✅ .user-card__avatar
✅ .user-card--featured
✅ .user-card__name--highlighted

❌ .user-card__header__title       (3 levels)
❌ .user-card .user-card__avatar   (descendant selector)
❌ div.user-card                   (tag-qualified)
```

## How to Use

Read individual rule files for detailed explanations and code examples:

```
rules/bem-block-selector.md
rules/bem-max-nesting.md
rules/bem-split-components.md
rules/bem-element-naming.md
rules/bem-modifier-patterns.md
rules/bem-no-cascading.md
```

Each rule file contains:
- Brief explanation of why it matters
- Incorrect code examples (CSS, SCSS, SASS)
- Correct code examples (CSS, SCSS, SASS)
- Angular component integration patterns
- Summary table and references

## Full Compiled Document

For the complete guide with all rules expanded: `AGENTS.md`

Overview

This skill is an Angular + BEM CSS best-practices guide that helps teams create reusable, component-scoped styles and shareable front-end code. It enforces one BEM block per component, limits nesting to two levels, and ensures semantic element and modifier naming. Use it to standardize styling during development, review, or refactoring of Angular components.

How this skill works

The skill inspects component selectors and style rules to verify that each Angular component maps to a single BEM block name and that selectors are flat (no descendant or tag-qualified selectors). It flags deeper-than-allowed nesting, recommends extracting child components when structure is too complex, and validates modifier and element naming patterns. The guidance includes bad/good examples and actionable fixes for CSS, SCSS, and SASS.

When to use it

  • Writing CSS/SCSS/SASS for new Angular components
  • Reviewing component styles during code review
  • Refactoring legacy styles into a component-based architecture
  • Designing a CSS architecture for a new Angular project
  • Deciding whether to split a component because styles are too deep

Best practices

  • One component = one BEM block; block name should match the component selector (minus prefix)
  • Limit nesting to block and block__element only; extract child components for deeper structure
  • Use semantic, descriptive kebab-case for elements (describe what, not how)
  • Use --modifier for variants and states, always paired with the base class
  • Keep selectors flat: avoid descendant, child, and tag-qualified selectors

Example use cases

  • Converting a monolithic component with many nested selectors into multiple smaller components with clear BEM blocks
  • Enforcing consistent class naming during a pull request review to avoid cascade and specificity issues
  • Refactoring SCSS that uses nested selectors into flat BEM classes to improve reuse and portability
  • Designing a component library where each exported component exposes a predictable BEM API
  • Implementing class bindings in Angular templates using correct modifier patterns for stateful variants

FAQ

What if I need more than two levels of styling?

Extract the deeply nested part into a child component with its own BEM block. This keeps selectors flat and components reusable.

Can I use tag-qualified selectors for global utilities?

Avoid tag-qualified selectors for component styles. Reserve global utilities for a separate layer and keep component styles scoped to BEM classes.