CSS Variables (also called Custom Properties) are one of the most powerful features in modern CSS. They help you write clean, reusable, and maintainable styles.
In this guide, youβll learn:
- What CSS Variables are
- Why we need them
- How they work
- Real-world examples
- Best practices
What Are CSS Variables?
CSS Variables are custom values that you define and reuse in your CSS.
π They start with -- and are accessed using var().
Basic Example
:root {
--primary-color: blue;
}button {
background-color: var(--primary-color);
}
π Output:
- Button background becomes blue
Why Do We Need CSS Variables?
Before CSS Variables, we had problems like:
β Repeating same values everywhere
β Difficult to update styles
β No dynamic theming
Example Without Variables
button {
background-color: blue;
}.header {
color: blue;
}
π If you want to change blue β red, you must update everywhere β
With CSS Variables
:root {
--primary-color: blue;
}button {
background-color: var(--primary-color);
}.header {
color: var(--primary-color);
}
π Change once β updated everywhere β
How CSS Variables Work
πΉ Defining Variables
:root {
--main-color: green;
}
π :root = global scope
πΉ Using Variables
p {
color: var(--main-color);
}
Scope of CSS Variables
πΉ Global Scope
:root {
--color: red;
}
π Available everywhere
πΉ Local Scope
.card {
--color: blue;
}.card p {
color: var(--color);
}
π Only inside .card
Updating Variables Dynamically
You can update variables using JavaScript:
document.documentElement.style.setProperty('--primary-color', 'red');
π Instantly updates UI π₯
Real-World Use Case: Theme Switching
Light Theme
:root {
--bg-color: white;
--text-color: black;
}
Dark Theme
.dark {
--bg-color: black;
--text-color: white;
}
Usage
body {
background-color: var(--bg-color);
color: var(--text-color);
}
π Just toggle .dark class β theme changes instantly
Fallback Values (Very Important)
color: var(--primary-color, red);
π If variable not defined β fallback to red
Advanced Example
:root {
--spacing: 10px;
}.container {
padding: calc(var(--spacing) * 2);
}
π Works with calculations π₯
π CSS Variables vs SCSS Variables
| Feature | CSS Variables | SCSS Variables |
|---|---|---|
| Runtime change | β Yes | β No |
| Works in browser | β Yes | β Precompiled |
| Dynamic themes | β Yes | β No |
Best Practices
- Use meaningful names (
--primary-color) - Define global variables in
:root - Avoid overusing variables
- Use for themes and reusable values
Where CSS Variables Are Used
- React (MUI, Tailwind configs)
- Theming systems
- Design systems
- Dark/light mode
Interview Tip
If asked:
βWhat are CSS variables?β
π Answer:
βCSS variables are reusable custom properties that allow dynamic styling and runtime updates in CSS.β
π Final Summary
- CSS Variables improve maintainability
- They support dynamic updates
- Useful for themes and reusable styles
- Work directly in browser
Related Articles
- Showing Greeting Message using ReactJS
- Solution: Jest encountered an unexpected token and failing at β- import { DemoContainer } from β@mui/x-date-pickers/internals/demoβ
- Solution: TypeError: Cannot read properties of undefined (reading βDemoContainerβ) and failing because of components={[βDatePickerβ, βTimePickerβ]} in the DemoContainer tag.
π‘ Found this helpful? Subscribe to get simple frontend tutorials, real-world examples, and developer tips. Happy Coding!