Lab 14: CSS Architecture & BEM
Objective
Write maintainable, scalable CSS using the BEM methodology, CSS cascade layers, utility classes, logical properties, and print styles β the professional practices that separate hobby projects from production codebases.
Background
As CSS grows beyond 1000 lines, naming conflicts, specificity wars, and tangled styles become common. CSS architecture solves this with methodologies (BEM), modern cascade control (@layer), and structured organization. This lab teaches the patterns used in large-scale production CSS.
Time
30 minutes
Prerequisites
Lab 12: CSS Variables & Theming
Tools
docker run --rm -it -v /tmp:/workspace zchencow/innozverse-htmlcss:latest bashLab Instructions
Step 1: BEM Methodology
Write this file:
π‘ BEM stands for Block__Element--Modifier. Block = standalone component (
.card). Element = part of block (.card__title). Modifier = variation (.card--featured). BEM classes are long but self-documenting β you know exactly where.card__title--largelives without reading the HTML. No specificity wars because all selectors have equal specificity (single class).
πΈ Verified Output:
Step 2: CSS Specificity & The Cascade
Write this file:
π‘ Specificity formula: Count (inline, ID, class/attr/pseudo-class, element/pseudo-element). Higher number wins. Same specificity? Last declaration wins (cascade order). Keep specificity flat β prefer single classes. The "specificity war" anti-pattern: chaining classes to override other classes, leading to ever-escalating selectors.
πΈ Verified Output:
Step 3: CSS Cascade Layers (@layer)
Write this file:
π‘
@layersolves specificity wars. Declare layer order first:@layer reset, base, components, utilities. Later layers always beat earlier ones, regardless of specificity. Unlayered CSS beats all layers. This means you can write reset rules with high specificity selectors without worrying about overriding component styles.
πΈ Verified Output:
Step 4: Utility Classes vs Component Classes
Write this file:
π‘ When to use which: Utility-first (Tailwind) is great for rapid prototyping and small teams β CSS barely grows. Component CSS (BEM) is better for large teams and design systems β HTML stays clean and semantic. Most modern projects use a hybrid: component classes for major UI elements, utilities for spacing/layout tweaks.
πΈ Verified Output:
Step 5: CSS Reset & Normalize
Write this file:
π‘ Every project needs a reset. Without one, you'll fight browser inconsistencies constantly. The 8-rule modern reset above (
box-sizing,margin: 0,font: inherit, etc.) is minimal and solves 95% of cross-browser issues. Add it as your first CSS.
πΈ Verified Output:
Step 6: CSS Logical Properties
Write this file:
π‘ Logical properties future-proof your CSS for internationalization.
margin-inline-startmeans "start side of the inline axis" β left in English (LTR), right in Arabic (RTL). If you ever need to support RTL languages, logical properties mean zero changes to your layout CSS. Usetext-align: startinstead oftext-align: leftas a habit.
πΈ Verified Output:
Step 7: Print Styles
Write this file:
π‘ Every serious website needs print styles. Users print receipts, articles, and documentation. Key rules: hide nav/ads/buttons, remove shadows/backgrounds (ink savings), show link URLs with
::after, usebreak-inside: avoidto prevent cards splitting across pages, and set@page { margin: 1in }for proper margins.
πΈ Verified Output:
Step 8: Capstone β Well-Organized Component Stylesheet
Write this file:
π‘ Capstone architecture summary: @layer controls cascade order β Design tokens define all values β BEM names all components β Logical properties enable RTL β Print styles make it paper-ready. This structure scales to 100K lines of CSS without specificity conflicts or cascade surprises. Real-world frameworks like Bootstrap, Tailwind, and Open Props use variations of this exact pattern.
πΈ Verified Output:
Verification
Summary
BEM naming
.block__element--modifier
Self-documenting, no conflicts
Specificity
Prefer single classes
No specificity wars
@layer
@layer reset, base, components
Explicit cascade control
Utility classes
.flex, .mt-4
Rapid, consistent tweaks
CSS Reset
8-rule modern reset
Cross-browser consistency
Logical properties
margin-inline, inline-size
RTL-ready layout
Print styles
@media print
Paper-friendly output
Further Reading
Last updated
