CSS · CSS Advanced · Lesson 56
CSS Variables
Custom properties with var().
CSS variables (custom properties) store values you can reuse. They are declared with two dashes and read with var().
Example
<style>
:root {
--main-color: #04AA6D;
--radius: 10px;
}
.btn { background: var(--main-color); color: white; border: none; padding: 10px 20px; border-radius: var(--radius); }
.dark { --main-color: #333; }
</style>
<button class="btn">Uses the variable</button>
<div class="dark"><button class="btn">Overridden inside .dark</button></div>
Tip: Variables declared on
:root are global. Unlike preprocessor variables, CSS variables can be changed at runtime with JavaScript.Keep reading “CSS Variables”
Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.