Episode 1 — Fundamentals / 1.10 — CSS Architecture and Project Structure

1.10 — CSS Architecture & Project Structure: Quick Revision

Compact cheat sheet. Print-friendly.

How to use this material (instructions)

  1. Skim top-to-bottom in one pass before interviews or exams.
  2. If a row feels fuzzy — reopen the matching lesson: README.md1.10.a1.10.e.
  3. Drills1.10-Exercise-Questions.md.
  4. Polished answers1.10-Interview-Questions.md.

1.10.a BEM Naming Convention

BEM Syntax

PartPatternExample
Block.block.card
Element.block__element.card__title
Modifier.block--modifier.card--featured
Element + Modifier.block__element--modifier.card__title--large

Key Rules

  • Elements belong to the block, never nested: .card__title not .card__body__title.
  • Always apply the base class alongside a modifier: class="card card--featured".
  • Every BEM selector is a single class → specificity is always 0-1-0.
  • SASS & compiles flat: &__title.card__title.

BEM Alternatives

NameCore Idea
OOCSSSeparate structure from skin
SMACSSCategorise: base, layout, module, state, theme
ITCSSLayer by specificity (inverted triangle)

1.10.b Component-Based CSS

Component API (3 Layers)

Base class       →  .alert             (default appearance)
Modifiers        →  .alert--success    (visual variant)
States           →  .alert.is-visible  (dynamic condition)

Principles

  • One component = one file, one job. No layout logic inside.
  • Never style bare tags inside a component (.card h2 { } leaks).
  • Compose complex UI from small independent components.
  • Spacing / positioning = parent's responsibility.

Scoping Strategies

StrategyMechanism
BEMConvention (developer discipline)
CSS ModulesBuild-tool hashing (.card.card_a3f2x)
Vue scopedAuto-added [data-v-xxxx] attribute
Shadow DOMTrue browser encapsulation
CSS-in-JSRuntime/build-time hashed styles

1.10.c Utility vs Component Classes

Side-by-Side

Component:  <button class="btn btn--primary">Go</button>
Utility:    <button class="bg-blue-600 text-white py-2 px-4 rounded">Go</button>

Comparison Table

UtilityComponent
CSS per class1 declarationMany declarations
HTML verbosityHighLow
CSS file sizeSmall (purged)Can grow
ConsistencyToken-enforcedConvention-dependent
RefactoringChange HTMLChange CSS

Utilities ≠ Inline Styles

  • Utilities support media queries (md:flex), pseudo-classes (hover:bg-blue-700), and design constraints (only mt-2, mt-4 — not arbitrary values).

Hybrid Rule of Thumb

  • Repeated patterns → component class.
  • One-off spacing / alignment → utility class.
  • Layout primitives (flex, grid, gap) → utility class often wins.

1.10.d Real-World Folder Structure

The 7-1 SASS Pattern

scss/
├── abstracts/     ← variables, mixins, functions (no output)
├── vendors/       ← third-party (normalize, etc.)
├── base/          ← resets, typography, global defaults
├── layout/        ← header, footer, grid, sidebar
├── components/    ← button, card, modal, form
├── pages/         ← page-specific overrides
├── themes/        ← dark mode, high contrast
└── main.scss      ← single entry, imports all ↑

Import Order

abstracts → vendors → base → layout → components → pages → themes

Abstracts first (variables available everywhere). Increasing specificity left → right.

File Naming

ConventionExample
Underscore prefix (SASS partial)_button.scss
Kebab-casefeature-list.css
Match BEM block name_card.scss.card

Modern Frameworks

ToolStyles live…
CSS ModulesNext to component: Button.module.css
Vue SFCInside .vue file: <style scoped>
Tailwindtailwind.config.js + minimal base.css
CSS-in-JSInside JS/TS component files

1.10.e Debugging with DevTools

Debugging Checklist

StepAction
1Inspect element → Styles panel
2Look for struck-through rules (overridden)
3If rule is missing → selector is wrong or stylesheet not loaded
4Check Computed panel for final resolved values
5Click arrow on computed value to jump to winning rule

Quick Tools

Tool / TrickPurpose
* { outline: 1px solid red; }Find overflow / layout issues (doesn't affect box model)
:hov button in Styles toolbarForce :hover, :focus, :active states
Flex / Grid badgesVisual axis, track, gap, and space inspection
Rendering → Layout Shift RegionsFlash blue on CLS shifts
Performance panel (green bars)Paint cost; purple bars = layout / reflow

Common Bugs → Fixes

BugFix
Rule struck-throughHigher-specificity rule wins — find it above in Styles
z-index not workingElement needs position set; check stacking context
Flex item overflowmin-width: auto default → set min-width: 0
Grid column blowout1fr with unbreakable content → use minmax(0, 1fr)
Horizontal scrollbarLikely 100vw + scrollbar or negative margins
Collapsed marginsAdjacent vertical margins merge — use padding or gap instead

One-Line Cheat Sheet

BEM = block__element--modifier  |  Flat specificity, grepable names
Component = one file, one job   |  Base + modifier + state API
Utility = one class, one rule   |  Composed in HTML, token-constrained
Folders = abstracts → vendors → base → layout → components → pages → themes
Debug = Inspect → Styles → Computed → outline trick → force states → perf panel