Learn how to define and use CSS custom properties (variables) with the -- syntax and var() function for maintainable, themeable stylesheets.
CSS custom properties (often called CSS variables) let you define reusable values that can be referenced throughout your stylesheet. They are declared with a double-hyphen prefix -- and read with the var() function.
var(--primary-color) is more meaningful than #3498db.Variables are declared on a selector. The most common place is :root (equivalent to <html>) so they are available globally:
:root {
--primary-color: #3498db;
--spacing-md: 16px;
--font-main: "Inter", sans-serif;
}Reference a variable with var():
.button {
background-color: var(--primary-color);
padding: var(--spacing-md);
font-family: var(--font-main);
}<style>
:root {
--primary: #3498db;
--secondary: #2ecc71;
--text-color: #333;
--bg-color: #ffffff;
--radius: 8px;
--spacing: 16px;
}
body {
color: var(--text-color);
background-color: var(--bg-color);
font-family: sans-serif;
padding: var(--spacing);
}
.card {
border: 2px solid var(--primary);
border-radius: var(--radius);
padding: var(--spacing);
margin-bottom: var(--spacing);
}
.card h3 {
color: var(--primary);
}
.btn {
background-color: var(--secondary);
color: white;
border: none;
padding: 8px var(--spacing);
border-radius: var(--radius);
cursor: pointer;
}
</style>
<div class="card">
<h3>CSS Variables</h3>
<p>Change --primary to update all blue elements at once.</p>
<button class="btn">Click Me</button>
</div>The var() function accepts an optional second argument — a fallback value used if the variable is not defined:
color: var(--accent-color, #e74c3c);If --accent-color is not defined in any ancestor, #e74c3c is used instead.
Custom properties follow the normal cascade and inherit down the DOM tree. You can override a variable for a specific subtree:
:root { --bg: white; }
.dark-section { --bg: #1a1a1a; }
.card {
background: var(--bg);
/* White in general, dark inside .dark-section */
}Because custom properties are live, you can update them at runtime:
// Change a global variable
document.documentElement.style.setProperty('--primary', '#e74c3c');
// Change a scoped variable
element.style.setProperty('--card-bg', '#f0f0f0');This makes CSS variables the foundation of dynamic theming — you can build a complete dark mode toggle without changing any CSS selectors.
What happens when you use `var(--undefined-var, blue)` and `--undefined-var` is not declared anywhere?