Modern applications allow users to personalize UI colors at runtime — without rebuilding the app or reloading the page. The best way to achieve this in Angular is by using CSS variables, real-time color pickers, and a theme service that writes styles directly to :root.
Below is a complete, practical, production-ready guide.
Architecture Overview (smaller header)
Angular App
|
|— ThemeService (writes CSS variables at runtime)
|
|— Theme Editor Component (color picker UI)
|
|— Global Styles (CSS variables in :root)
Workflow Diagram (smaller header)
User selects color →
Color Picker emits value →
ThemeService.setVar('--primary', value) →
CSS variable updates instantly →
All components using var(--primary) update live
ER Diagram (Optional – For Persisting Themes in DB)
(Skip if you don’t store themes on backend)
+----------------------+
| Theme |
+----------------------+
| ThemeId (PK) |
| UserId |
| PrimaryColor |
| SecondaryColor |
| BackgroundColor |
| AccentColor |
+----------------------+
Sequence Diagram (smaller header)
User → ThemeEditor: selects new color
ThemeEditor → ThemeService: setCssVariable('--primary', '#4caf50')
ThemeService → DOM: write variable to :root
DOM → UI Components: instantly reflect new color
Step-by-Step Practical Implementation (Frontend Only)
1. Define Global CSS Variables
Add to styles.css:
:root {
--primary: #1976d2;
--secondary: #424242;
--background: #ffffff;
--accent: #ff4081;
}
body {
background: var(--background);
color: var(--secondary);
}
.btn-primary {
background: var(--primary);
color: #fff;
}
2. Create ThemeService (writes variables live)
theme.service.ts
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class ThemeService {
setVar(variable: string, value: string) {
document.documentElement.style.setProperty(variable, value);
}
getVar(variable: string): string {
return getComputedStyle(document.documentElement).getPropertyValue(variable);
}
// store theme in local storage
saveTheme(theme: Record<string, string>) {
localStorage.setItem('app-theme', JSON.stringify(theme));
}
loadTheme() {
const saved = localStorage.getItem('app-theme');
if (!saved) return;
const theme = JSON.parse(saved);
Object.keys(theme).forEach(v => this.setVar(v, theme[v]));
}
}

Join the conversation! Your thoughts help the community grow.