Theme Based Design
The same component can use different colors by changing CSS variables.
Clear, interactive, and structured CSS lessons designed for beginners.
CSS Variables allow you to store reusable CSS values such as colors, spacing, font sizes, and other design values in one place.
CSS Variables, also called CSS Custom Properties, are reusable values that can be defined once and used in multiple places in a stylesheet.
They are especially useful for maintaining a consistent design system and making large websites easier to manage.
This visual represents a reusable color system. Hover over the image to see the colors smoothly change using CSS variables.
CSS variables help maintain consistent colors and design values across a website.
:root {
--primary-color: #2563eb;
--text-color: #212529;
}
.button {
background-color: var(--primary-color);
color: #ffffff;
}
A CSS variable starts with two hyphens:
--.
The var() function is then used to access
the stored value.
The :root selector is commonly used to define
global CSS variables so they can be reused throughout
the stylesheet.
:root {
--primary-color: #2563eb;
--secondary-color: #7c3aed;
--text-color: #212529;
--spacing: 20px;
}
Variables defined in :root can generally
be accessed throughout the document.
:root {
--primary-color: #2563eb;
}
.button {
background-color: var(--primary-color);
}
CSS variables are not limited to colors. You can store spacing, border radius, font sizes, shadows, and many other reusable values.
:root {
--primary-color: #2563eb;
--card-radius: 12px;
--card-padding: 24px;
--heading-size: 28px;
}
:root {
--primary-color: #2563eb;
--card-background: #ffffff;
--text-color: #212529;
--card-radius: 12px;
}
.course-card {
background-color: var(--card-background);
color: var(--text-color);
border: 2px solid var(--primary-color);
border-radius: var(--card-radius);
}
This card uses CSS variables for its color, border, and border radius.
One major advantage of CSS variables is that changing one variable can update multiple elements that use it.
:root {
--primary-color: #2563eb;
}
.button {
background-color: var(--primary-color);
}
.card {
border-color: var(--primary-color);
}
.heading {
color: var(--primary-color);
}
Instead of changing the same color in many places, you can change the variable once.
CSS variables are very useful for creating themes. Different themes can use different variable values.
:root {
--primary-color: #2563eb;
--background-color: #ffffff;
--text-color: #212529;
}
.dark-theme {
--primary-color: #a78bfa;
--background-color: #111827;
--text-color: #f9fafb;
}
The same component can use different colors by changing CSS variables.
Hover over the theme card to see the variable values change visually.
Variables do not always need to be global. A variable can also be defined inside a specific element.
.course-card {
--card-color: #7c3aed;
border-color: var(--card-color);
}
A locally defined variable can be used inside the element and its applicable descendants.
You can provide a fallback value in case a CSS variable is not available.
If --button-color is unavailable,
the browser can use #2563eb as the
fallback value.
CSS variables can also be used inside functions such as
calc().
:root {
--page-padding: 20px;
}
.container {
width: calc(100% - (var(--page-padding) * 2));
}
| Normal CSS Value | CSS Variable |
|---|---|
| Value is repeated manually | Value can be reused |
| Changing values can take more effort | Change the variable once |
| Less flexible for themes | Useful for theme systems |
| Can lead to repetition | Reduces repeated values |
:root.
CSS variables make styling easier to maintain, but they should still be named clearly and organized logically. A good naming system becomes especially important in large projects.
CSS Variables, also called Custom Properties, allow
reusable CSS values to be defined and accessed using
var(). They are useful for colors,
spacing, typography, themes, component styling,
and maintainable design systems.