CSS Mastery — From Zero to Beautiful CSS Variables (Custom Properties)
1 / 10
Next
CSS Variables (Custom Properties) ~10min

CSS Custom Properties (Variables)

CSS Variables let you store values once and reuse them everywhere. Perfect for theming and consistency.

Declaring variables

:root {
  --primary:    #00bcd4;
  --text:       #0d1b2a;
  --radius:     10px;
  --spacing-md: 16px;
}

Variables on :root are global. The -- prefix is required.

Using variables

button {
  background: var(--primary);
  border-radius: var(--radius);
  padding: var(--spacing-md);
}

/* Fallback if undefined */
color: var(--accent, #ff6b6b);

Dynamic theming with JS

document.documentElement.style
    .setProperty("--primary", "#ff6b6b");
Preview