Saltar al contenido principal
← 🎨 CSS as a design system·Módulo B8 · Lección 10
TareaBuild 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); } }.

Estructura de archivos de componentes para 2026

75 XP5 min
Teoría

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.

🔒

Regístrate para empezar a programar

La teoría está abierta a todos. El editor, la vista previa y el check se desbloquean con una prueba gratis de 7 días — se requiere tarjeta, cancela cuando quieras.

Registrarse — prueba gratis →

Las primeras 10 lecciones de cada ruta son gratis. No se necesita tarjeta.

← AnteriorSiguiente lección →

Recibe un consejo de Python o web al día — por email

Corto, escrito a mano, sin spam. Anula la suscripción en un clic.