Implementing light and dark themes used to be messy: duplicate styles, endless overrides, and hardcoded colors scattered across files. With Sass maps and mixins, you can build a clean, scalable theming system that stays maintainable and developer-friendly.
In this article, we’ll break down a practical approach to theming step by step—in a way beginners can follow.
You’ll need Sass in your project. Sass is a CSS preprocessor that makes writing and managing styles more structured. See the docs: Sass Lang: CSS with superpowers.
The problem with traditional theming
Most developers start like this:
.light {
background: white;
color: black;
}
.dark {
background: black;
color: white;
}
This works… until your app grows. Imagine 50+ colors, multiple components, and ongoing design updates—and suddenly you’re managing chaos.
The idea: centralize everything
Instead of scattering styles, we:
- Define all theme values in one place
- Use Sass maps to store them
- Use a mixin to generate CSS variables
- Switch themes by toggling a class
Step 1: Understanding Sass maps
A Sass map is like a JavaScript object:
$light-theme: (
text-color-1: #141414,
surface-bg-color-1: #ffffff,
);
$dark-theme: (
text-color-1: #f0f0f0,
surface-bg-color-1: #141414,
);
Think of it as a dictionary of design tokens—a single source of truth. We’ve created two maps, $light-theme and $dark-theme. Each can hold text colors, background colors, border colors, shadows, and semantic colors (success, warning, error). Everything stays organized in one place.
Step 2: Accessing map values
Sass provides a function:
map.get(<theme-map-name>, <key>)
Example:
map.get($light-theme, text-color-1)
This returns:
#141414
So instead of hardcoding values, you query your theme.
Step 3: The power of mixins
Now comes the real magic—our mixin:
@mixin apply-theme($theme-map) {
--text-color-1: #{map.get($theme-map, text-color-1)};
}
What’s happening here?
We’re converting Sass values into CSS custom properties. Each theme map gets transformed into a set of variables like:
--text-color-1: #141414;
--surface-bg-color-1: #ffffff;
Why is this powerful? Because CSS variables:
- Work at runtime
- Can be changed dynamically
- Are accessible anywhere in your app
Step 4: Applying themes
We apply the mixin like this:
body {
@include apply-theme($light-theme);
}
body.dark {
@include apply-theme($dark-theme);
}
This sets the light theme as the default. When the .dark class is added to body, the dark theme is applied instantly.
Step 5: Using the variables
In components, we don’t reach for Sass maps or Sass variables anymore. We use CSS variables:
.card {
background: var(--surface-bg-color-1);
color: var(--text-color-1);
}
That separation is the key: Sass maps store theme values; the mixin converts those maps into CSS variables.
Why this approach scales
1. Single source of truth
All colors live in one place.
2. Easy theme switching
Toggle a class:
document.body.classList.toggle('dark')
Or use another library for the same idea. In Vue projects, I use the useDark() composable from VueUse.
3. Zero style duplication
No .dark .card {} overrides needed.
4. Semantic design system
We’re not sprinkling random colors like #fff. We’re using names such as:
text-color-errorsurface-bg-color-success-light
That keeps the UI consistent, readable, and design-friendly.
5. Future-proof
Adding a new theme? Define $special-theme: ( ... ); and plug it into the same mixin.
Final thoughts
This pattern shows up in design systems, component libraries, and large-scale frontend apps because it reduces bugs, improves maintainability, and keeps design and code aligned.
What we’ve built here is more than light/dark mode—it’s a scalable theming architecture. When you’re building modern web apps, this approach isn’t just helpful—it’s essential.
Thanks for reading—I hope you learned something new. See you next time.



