CSS Variables
CSS
What are CSS Variables?
CSS Variables let you store values (like colors, sizes, fonts, etc.) in reusable names, so you can update them in one place and use them throughout your stylesheet.
1. Define a variable
You define variables inside a selector — usually :root
so they apply globally.
2. Use the variable
You use it with the var()
function.
Why Use CSS Variables?
- Easy to update global styles (colors, sizes)
- Keeps code clean and consistent
- Great for theming (light/dark mode)
- Reusable across components
Example
We can change --primary-color
in one place and it updates everywhere it’s used!
Scoped Variables
You can also define variables inside specific selectors:
These variables apply only inside .card
.