Skip to main content
← 🎨 CSS as a design systemΒ·Module B8 Β· Lesson 10
TaskBuild a card using the 5-layer pattern in one file (since we can't have multiple files in this exercise). @layer tokens, base, components. @layer tokens { :root { --primary: #3B82F6; --bg: white; --radius: 12px; } }. @layer base { body { font-family: system-ui, sans-serif; padding: 24px; } }. @layer components { .card { padding: 20px; background: var(--bg); border-radius: var(--radius); border: 1px solid #e5e7eb; } .card h3 { margin: 0; color: var(--primary); } }.

Component file structure for 2026

75 XP5 min
Theory

The "5 files" structure that scales

/styles
  β”œβ”€β”€ reset.css         # box-sizing, zero margins
  β”œβ”€β”€ tokens.css        # :root { --primary: ... }
  β”œβ”€β”€ base.css          # body, h1-h6, a defaults
  β”œβ”€β”€ components/       # one file per component
  β”‚   β”œβ”€β”€ button.css
  β”‚   β”œβ”€β”€ card.css
  β”‚   β”œβ”€β”€ nav.css
  β”‚   └── ...
  └── utilities.css     # .text-center, .sr-only, etc.

Then a single main.css imports them in @layer order:

@layer reset, tokens, base, components, utilities;

@import url("./reset.css") layer(reset);
@import url("./tokens.css") layer(tokens);
@import url("./base.css") layer(base);
@import url("./components/button.css") layer(components);
@import url("./components/card.css") layer(components);
@import url("./utilities.css") layer(utilities);

Why this structure

  • Layers mean specificity wars are dead.
  • One file per component scales to 100+ components without merge conflicts.
  • Tokens centralized β€” change a color, every component updates.
  • Utilities last β€” overrides ANY component when applied to a specific element.

When to add @scope

Add @scope (.card) inside components/card.css to namespace child selectors WITHOUT writing .card 50 times.

Tailwind users: same structure, fewer files

Tailwind users don't ship many .css files. Their structure is:

/styles
  β”œβ”€β”€ tailwind.css           # @tailwind base; @tailwind components; @tailwind utilities;
  └── tokens.css             # @theme { --color-primary: ... }

Tailwind 4 added a @theme directive for tokens, replacing tailwind.config.js for most projects.

πŸ”’

Sign up to start coding

Theory is open to everyone. The interactive editor, live preview, and check are unlocked with a 7-day free trial β€” card required, cancel anytime.

Sign up β€” free trial β†’

First 10 lessons in each track are free. No card needed for those.

← PreviousNext lesson β†’

Get one Python or web tip a day β€” by email

Short, hand-written, no spam. Unsubscribe in one click.