Introduction
Modern frontend development is no longer about writing isolated CSS files and static UI components.
Today’s interfaces must be scalable, theme-aware, and dynamic while maintaining performance and design consistency.

This is where CSS Custom Properties (CSS Variables) shine.

In this blog, we will build a modern UI kit architecture using CSS Custom Properties that supports:
-
Centralised design tokens
-
Live theme switching (light/dark)
-
Clean UI/UX separation
-
Minimal JavaScript
-
Production-ready scalability
This approach is widely used in modern SaaS dashboards, design systems, and component libraries.
What Are CSS Custom Properties?
CSS Custom Properties are runtime CSS variables that live in the browser, not during build time.
1:root {
2 --primary-color: #6366f1;
3 --background-color: #ffffff;
4}Unlike Sass or Less variables, CSS Custom Properties:
-
Can be updated dynamically
-
Cascade like normal CSS
-
Can be modified using JavaScript
-
They are perfect for theming and design systems
Why Modern UI Kits Use Design Tokens
Design tokens are single sources of truth for UI values like:
-
Colors
-
Spacing
-
Typography
-
Border radius
-
Shadows
Instead of hardcoding values repeatedly, components reference tokens.
This makes UI kits:
-
Easier to maintain
-
Easier to redesign
-
Easier to scale
Defining Design Tokens (Foundation Layer)
We start by explaining all visual values in one place.
1/* Design Tokens */
2:root {
3 /* Colors */
4 --color-primary: #6366f1;
5 --color-secondary: #22c55e;
6 --color-danger: #ef4444;
7
8 /* Backgrounds */
9 --bg-primary: #ffffff;
10 --bg-secondary: #f9fafb;
11
12 /* Text */
13 --text-primary: #111827;
14 --text-secondary: #6b7280;
15
16 /* Spacing */
17 --space-xs: 4px;
18 --space-sm: 8px;
19 --space-md: 16px;
20 --space-lg: 24px;
21
22 /* Border Radius */
23 --radius-sm: 6px;
24 --radius-md: 10px;
25
26 /* Shadows */
27 --shadow-md: 0 10px 25px rgba(0, 0, 0, 0.08);
28}UX Impact:
Changing one token updates the entire UI instantly.
Base Styles Using Tokens
Base styles ensure consistency across all components.
1body {
2 background-color: var(--bg-primary);
3 color: var(--text-primary);
4 font-family: system-ui, sans-serif;
5 margin: 0;
6}
7
8button {
9 font-family: inherit;
10}This ensures your UI kit feels cohesive and professional.
Building Reusable UI Components
Button Component
1.btn {
2 padding: var(--space-sm) var(--space-md);
3 border-radius: var(--radius-md);
4 border: none;
5 background-color: var(--color-primary);
6 color: white;
7 cursor: pointer;
8 box-shadow: var(--shadow-md);
9 transition: transform 0.2s ease, background-color 0.2s ease;
10}
11
12.btn:hover {
13 transform: translateY(-2px);
14}Card Component
1.card {
2 background-color: var(--bg-secondary);
3 padding: var(--space-lg);
4 border-radius: var(--radius-md);
5 box-shadow: var(--shadow-md);
6}Notice how no raw values are used — only tokens.
Creating Multiple Themes (Dark Mode)
Instead of rewriting components, we override tokens.
1[data-theme="dark"] {
2 --bg-primary: #0f172a;
3 --bg-secondary: #020617;
4 --text-primary: #e5e7eb;
5 --text-secondary: #94a3b8;
6 --color-primary: #818cf8;
7}This single block controls the entire dark theme UI.
Live Theme Switching with JavaScript
We toggle themes using a single HTML attribute.
1const toggleButton = document.querySelector("#themeToggle");
2
3toggleButton.addEventListener("click", () => {
4 const currentTheme = document.documentElement.getAttribute("data-theme");
5 const newTheme = currentTheme === "dark" ? "light" : "dark";
6
7 document.documentElement.setAttribute("data-theme", newTheme);
8 localStorage.setItem("theme", newTheme);
9});
10
11// Load saved theme
12const savedTheme = localStorage.getItem("theme");
13if (savedTheme) {
14 document.documentElement.setAttribute("data-theme", savedTheme);
15}Result:
Instant UI update without reload or re-render.
Complete HTML Example (Everything in One File)
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <title>Next-Gen UI Kit</title>
6
7 <style>
8 :root {
9 --color-primary: #6366f1;
10 --bg-primary: #ffffff;
11 --bg-secondary: #f9fafb;
12 --text-primary: #111827;
13 --radius-md: 10px;
14 --shadow-md: 0 10px 25px rgba(0,0,0,0.08);
15 }
16
17 [data-theme="dark"] {
18 --bg-primary: #0f172a;
19 --bg-secondary: #020617;
20 --text-primary: #e5e7eb;
21 --color-primary: #818cf8;
22 }
23
24 body {
25 background: var(--bg-primary);
26 color: var(--text-primary);
27 font-family: system-ui, sans-serif;
28 padding: 40px;
29 }
30
31 .btn {
32 padding: 10px 16px;
33 border-radius: var(--radius-md);
34 border: none;
35 background: var(--color-primary);
36 color: white;
37 cursor: pointer;
38 box-shadow: var(--shadow-md);
39 }
40
41 .card {
42 background: var(--bg-secondary);
43 padding: 24px;
44 margin-top: 24px;
45 border-radius: var(--radius-md);
46 box-shadow: var(--shadow-md);
47 }
48 </style>
49</head>
50<body>
51
52 <button id="themeToggle" class="btn">Toggle Theme</button>
53
54 <div class="card">
55 <h2>Next-Gen UI Kit</h2>
56 <p>Powered by CSS Custom Properties</p>
57 </div>
58
59 <script>
60 document.getElementById("themeToggle").addEventListener("click", () => {
61 const theme =
62 document.documentElement.getAttribute("data-theme") === "dark"
63 ? "light"
64 : "dark";
65 document.documentElement.setAttribute("data-theme", theme);
66 });
67 </script>
68
69</body>
70</html>UI/UX Benefits of This Architecture
-
No duplicated CSS
-
Centralised design control
-
Easy white-labeling
-
Smooth designer–developer collaboration
-
Framework-agnostic
Performance & Scalability
-
Zero layout reflow
-
No CSS recompilation
-
Native browser support
-
Works with React, Next.js, Vue, or Vanilla JS
This makes it production-ready and future-proof.
When to Use This UI Kit Pattern
-
SaaS dashboards
-
Portfolio systems
-
Design systems
-
Component libraries
-
Client white-label products
Final Thoughts
CSS Custom Properties are no longer optional in modern frontend development.
They enable UI systems that are:
-
Dynamic
-
Maintainable
-
Performant
-
Scalable
If you want to build professional, modern UI kits, this approach is essential.
🚀 Your frontend architecture just levelled up.