Saltar al contenido principal
← 🎨 CSS as a design system·Módulo B8 · Lección 5
TareaBuild .button with custom-property props (--bg, --fg, --pad-x, --pad-y) and three variants. .button.danger overrides --bg. .button.success overrides --bg. .button.large overrides --pad-x and --pad-y.

Propiedades personalizadas como API de componente

100 XP8 min
Teoría

Components that accept "props" via CSS vars

.button {
  --bg: #3B82F6;
  --fg: white;
  --pad-x: 20px;
  --pad-y: 10px;

  background: var(--bg);
  color: var(--fg);
  padding: var(--pad-y) var(--pad-x);
  border-radius: 8px;
  border: none;
}

/* Variants override the vars */
.button.danger { --bg: #DC2626; }
.button.success { --bg: #22C55E; }
.button.small { --pad-x: 12px; --pad-y: 6px; }
.button.large { --pad-x: 28px; --pad-y: 14px; }

The COMPONENT defines its API via internal custom properties. VARIANTS just override the props.

Why this beats classes-for-everything

Compare to the BEM way:

/* BEM — adds a new rule per variant */
.button { background: #3B82F6; padding: 10px 20px; }
.button--danger { background: #DC2626; }
.button--small { padding: 6px 12px; }

With BEM, the variant needs to know which CSS properties to override. With custom-property props, the component declares its API ONCE — variants just set the props.

Use cases

  • Spacing tokens (--gap, --padding).
  • Sizing tokens (--max-width, --min-height).
  • Visual tokens (--bg, --fg, --accent).
  • Animation timing (--duration).

The contract

  • Define vars at the component root (.button { --bg: ... }).
  • Use them in the component's CSS (background: var(--bg)).
  • Allow variants to OVERRIDE them.
  • Optionally use the fallback syntax var(--bg, fallback-default) for resilience.

This is THE modern way to build a CSS design system without preprocessors or CSS-in-JS.

🔒

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.