CSS Architecture: BEM, OOCSS & CSS Modules
Stop global CSS from polluting your entire project. Master BEM (Block Element Modifier), understand why keeping all selectors at flat specificity (0, 1, 0) prevents style wars, and compare CSS Modules with modern Utility-First CSS.
1. Mental Model: Modular Lego Bricks vs. The Tangled Yarn
The Tangled Ball of Global Yarn (No Architecture)
Writing generic selectors like .title or deep nesting like .sidebar div ul li a span. Six months later, nobody dares touch the CSS because modifying one class unintentionally breaks twelve other pages!
Modular Lego Bricks (BEM / Scoped Architecture)
Every component is an independent, encapsulated island. All styles are scoped strictly to the component. You can safely delete or refactor a component without fearing accidental collateral damage anywhere else!
2. The BEM Naming Convention: Block, Element, Modifier
The top-level container component. Standalone and reusable anywhere on the site.
A child part inside the block separated by double underscores (__). Tied to the block.
A visual variation or state separated by double dashes (--).
3. Interactive Studio: Live BEM Component Builder
<h4 class="card__title">BEM Component</h4>
<span class="card__badge">Active</span>
</div>
.card__title
Notice: Flat selector specificity (0, 1, 0) guarantees zero specificity overrides!
First-Year Student Traps: CSS Architecture
- 1.The Grandchild BEM Anti-Pattern: Never write
.card__header__title__icon! Elements should always be direct children of the block in class naming:.card__icon, regardless of how deep the HTML DOM tree nests. - 2.Overusing !important to Patch Specificity: When you start using
!importantto fix CSS bugs, you are writing an IOU against your future sanity. When another developer also needs to override your rule, they will have to write even higher specificity or chain multiple!importantflags!
CSS Debugging Tools & DevTools Mastery
Inspect computed styles, force hover pseudo-states, visualize CSS Grid overlays, and audit layout shifts.