CSS Architecture
CSS
What is CSS Architecture?
CSS architecture is how you structure your styles so they:
- Avoid conflicts
- Are easy to reuse
- Scale well with big projects
Popular methodologies include:
- BEM (Block Element Modifier)
- OOCSS (Object-Oriented CSS)
- SMACSS (Scalable and Modular Architecture for CSS)
1. BEM (Block Element Modifier)
Naming convention to keep code readable and organized.
- Format: .block .block__element .block--modifier
- Example:
Clear relationship between components and their parts.
2. OOCSS (Object-Oriented CSS)
Separate structure and skin. Focuses on reusable patterns.
- Principles:
- Separate structure from visual appearance
- Separate containers from content
- Example:
Reuse media for layout and card for design — modular and flexible.
SMACSS (Scalable and Modular Architecture for CSS)
SMACSS categorizes CSS into five types:
- Base – Default styles (e.g., body, h1, a)
- Layout – Page structure (e.g., .header, .sidebar)
- Module – Reusable components (e.g., .card, .btn)
- State – How things look in a specific state (e.g., .is-active, .is-hidden)
- Theme – Visual changes (e.g., .theme-dark)
Example:
Encourages separation of concerns and scalability.
Method | Key Idea | Strengths |
---|---|---|
BEM | Naming convention (block__element--modifier) | Easy to read, avoids conflicts |
OOCSS | Reusable, object-based design | DRY, modular |
SMACSS | Organize by role (base, layout, etc.) | Scalable, flexible architecture |