CSS variables store values you reuse, like brand colours.
Declare
:root {
--primary: #1f386b;
--radius: 8px;
}
Variable names start with two dashes. :root makes them available everywhere.
Use
background: var(--primary);
Fallback
color: var(--text, #333); uses #333 if --text is not defined.
Theming
Change a variable in one place (or inside a class like .dark) and every rule that uses it updates.
Example
Example — Theme with variables
HTML
<style>
:root { --bg: #ffffff; --text: #222; --primary: #1f386b; }
.dark { --bg: #1e1e1e; --text: #eee; --primary: #4a90e2; }
.panel { background: var(--bg); color: var(--text); padding: 16px; border: 2px solid var(--primary); border-radius: 8px; margin-bottom: 10px; }
.panel h3 { color: var(--primary); margin-top: 0; }
</style>
<div class="panel"><h3>Light theme</h3>Same CSS rules.</div>
<div class="panel dark"><h3>Dark theme</h3>Only the variables changed.</div>