CSS Variables (also called custom properties) let you store reusable values in CSS. They improve readability, reusability, and theming by keeping key values (colors, font sizes, spacing) in one place. They are declared using --variable-name and accessed using var(--variable-name).
Example use cases:
--, e.g. --main-bg.:root for global access.var(): var(--main-bg) returns the variable’s value.A CSS Variable is declared as a normal property whose name starts with --. The var() function is used to read its value, with optional fallback: var(--name, fallback).
/* Define global CSS variables in :root */
:root {
--main-bg: #fefefe;
--main-color: #333333;
--font-size: 18px;
}
/* Use the variables anywhere in the page */
body {
background-color: var(--main-bg);
color: var(--main-color);
font-size: var(--font-size);
}
/* Example with fallback value */
h1 {
color: var(--brand-color, #2563eb);
}
The variables --main-bg, --main-color, and --font-size are defined in :root, so they act like global settings. Any element can access them using var().
In the h1 rule, var(--brand-color, #2563eb) uses --brand-color if it exists; otherwise it falls back to #2563eb.
You can build light and dark themes by changing variable values on theme container classes. The component uses var() and automatically adapts to the active theme.
/* Define theme-specific variables on containers */
.light-theme {
--bg: #ffffff;
--text: #111111;
}
.dark-theme {
--bg: #111111;
--text: #ffffff;
}
/* Component that reads from the current theme */
.theme-box {
background: var(--bg);
color: var(--text);
padding: 15px;
border-radius: 8px;
margin-bottom: 10px;
}
When you wrap an element with class="theme-box light-theme", the variables from .light-theme are used. If you instead use class="theme-box dark-theme", the values from .dark-theme are applied.
By toggling between these theme classes (e.g., on a parent element), you can switch entire sections of the UI between light and dark modes without changing individual CSS rules.
CSS Variables can be updated dynamically using JavaScript. This is perfect for sliders, color pickers, live previews, or user-controlled themes.
// Update a global CSS variable at runtime
document.documentElement.style.setProperty("--main-color", "blue");
// Example: read the current value
const currentColor = getComputedStyle(document.documentElement)
.getPropertyValue("--main-color")
.trim();
document.documentElement targets the <html> element (same as :root in CSS). Using style.setProperty(), you can change --main-color at runtime, and all elements using var(--main-color) update instantly.
The getComputedStyle() API lets you read the effective value of a variable, which is useful for debugging or syncing UI controls with the current theme.
:root so they are globally available.var() for safer, backward-compatible styling..light-theme and .dark-theme.calc().:root (e.g., --primary, --bg, --radius) and apply them to a simple card layout.--primary) inside a special class (e.g., .warning-card) and see how only those cards change.calc() with a variable (e.g., padding: calc(var(--base-space) * 2);) and experiment with different base sizes.--primary-color based on a button click.--font-size) for small screens and observe how the text scales responsively.