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!