Let me create something that's NOT generic - I'll design an interesting toggle with:
A polished, animated theme switcher with localStorage persistence and system preference detection.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Theme Toggle</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
<style>
:root {
--bg: #f8f7f4;
--bg-secondary: #efeee9;
--fg: #1a1a1a;
--fg-muted: #6b6b6b;
--accent: #e85d04;
--accent-glow: rgba(232, 93, 4, 0.3);
--card: #ffffff;
--border: #d4d3cf;
--toggle-bg: #1a1a1a;
--sun-color: #fbbf24;
--moon-color: #94a3b8;
}
[data-theme="dark"] {
--bg: #0f0f0f;
--bg-secondary: #1a1a1a;
--fg: #f5f5f5;
--fg-muted: #8a8a8a;
--accent: #fb923c;
--accent-glow: rgba(251, 146, 60, 0.25);
--card: #1f1f1f;
--border: #2a2a2a;
--toggle-bg: #f5f5f5;
--sun-color: #fbbf24;
--moon-color: #6366f1;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Space Grotesk', sans-serif;
background-color: var(--bg);
color: var(--fg);
min-height: 100vh;
transition: background-color 0.5s ease, color 0.4s ease;
}
/* Background Pattern */
.bg-pattern {
position: fixed;
inset: 0;
background-image:
radial-gradient(circle at 20% 80%, var(--accent-glow) 0%, transparent 50%),
radial-gradient(circle at 80% 20%, var(--accent-glow) 0%, transparent 40%);
opacity: 0.6;
pointer-events: none;
z-index: 0;
transition: opacity 0.5s ease;
}
.grid-overlay {
position: fixed;
inset: 0;
background-image:
linear-gradient(var(--border) 1px, transparent 1px),
linear-gradient(90deg, var(--border) 1px, transparent 1px);
background-size: 60px 60px;
opacity: 0.3;
pointer-events: none;
z-index: 0;
}
/* Toggle Button */
.theme-toggle {
position: relative;
width: 72px;
height: 36px;
border-radius: 50px;
background: var(--toggle-bg);
border: 2px solid var(--border);
cursor: pointer;
overflow: hidden;
transition: background-color 0.3s ease, border-color 0.3s ease, transform 0.15s ease;
box-shadow:
0 2px 8px rgba(0, 0, 0, 0.1),
inset 0 1px 0 rgba(255, 255, 255, 0.1);
}
.theme-toggle:hover {
transform: scale(1.05);
border-color: var(--accent);
}
.theme-toggle:active {
transform: scale(0.95);
}
.theme-toggle:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 3px;
}
.toggle-track {
position: absolute;
inset: 3px;
border-radius: 50px;
background: linear-gradient(135deg, #87ceeb 0%, #5da9e9 100%);
transition: background 0.4s ease;
overflow: hidden;
}
[data-theme="dark"] .toggle-track {
background: linear-gradient(135deg, #1e1b4b 0%, #312e81 100%);
}
.toggle-stars {
position: absolute;
inset: 0;
opacity: 0;
transition: opacity 0.4s ease;
}
[data-theme="dark"] .toggle-stars {
opacity: 1;
}
.star {
position: absolute;
width: 2px;
height: 2px;
background: #ffffff;
border-radius: 50%;
}
.star:nth-child(1) { top: 8px; left: 10px; }
.star:nth-child(2) { top: 14px; left: 18px; width: 1.5px; height: 1.5px; }
.star:nth-child(3) { top: 6px; left: 25px; }
.star:nth-child(4) { top: 18px; left: 8px; width: 1px; height: 1px; }
.star:nth-child(5) { top: 20px; left: 22px; width: 1.5px; height: 1.5px; }
.toggle-thumb {
position: absolute;
top: 4px;
left: 4px;
width: 28px;
height: 28px;
border-radius: 50%;
background: var(--sun-color);
transition: transform 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55), background 0.3s ease;
z-index: 2;
display: flex;
align-items: center;
justify-content: center;
}
[data-theme="dark"] .toggle-thumb {
transform: translateX(36px);
background: var(--moon-color);
}
.toggle-icon {
width: 18px;
height: 18px;
position: relative;
}
/* Sun Icon */
.sun-icon {
position: absolute;
inset: 0;
transition: transform 0.4s ease, opacity 0.3s ease;
}
[data-theme="dark"] .sun-icon {
transform: rotate(180deg) scale(0);
opacity: 0;
}
.sun-ray {
position: absolute;
background: #ffffff;
border-radius: 1px;
}
.sun-center {
position: absolute;
top: 50%;
left: 50%;
width: 8px;
height: 8px;
background: #ffffff;
border-radius: 50%;
transform: translate(-50%, -50%);
}
.sun-ray {
width: 2px;
height: 4px;
}
.sun-ray:nth-child(2) { top: 0; left: 50%; transform: translateX(-50%); }
.sun-ray:nth-child(3) { bottom: 0; left: 50%; transform: translateX(-50%); }
.sun-ray:nth-child(4) { top: 50%; left: 0; transform: translateY(-50%); height: 2px; width: 4px; }
.sun-ray:nth-child(5) { top: 50%; right: 0; transform: translateY(-50%); height: 2px; width: 4px; }
.sun-ray:nth-child(6) { top: 2px; left: 2px; transform: rotate(-45deg); height: 3px; }
.sun-ray:nth-child(7) { top: 2px; right: 2px; transform: rotate(45deg); height: 3px; }
.sun-ray:nth-child(8) { bottom: 2px; left: 2px; transform: rotate(45deg); height: 3px; }
.sun-ray:nth-child(9) { bottom: 2px; right: 2px; transform: rotate(-45deg); height: 3px; }
/* Moon Icon */
.moon-icon {
position: absolute;
inset: 0;
transition: transform 0.4s ease, opacity 0.3s ease;
transform: rotate(-90deg) scale(0);
opacity: 0;
}
[data-theme="dark"] .moon-icon {
transform: rotate(0) scale(1);
opacity: 1;
}
.moon-body {
position: absolute;
top: 50%;
left: 50%;
width: 12px;
height: 12px;
background: #c7d2fe;
border-radius: 50%;
transform: translate(-50%, -50%);
}
.moon-crater {
position: absolute;
background: #a5b4fc;
border-radius: 50%;
}
.moon-crater:nth-child(2) { width: 3px; height: 3px; top: 4px; left: 3px; }
.moon-crater:nth-child(3) { width: 2px; height: 2px; top: 8px; left: 7px; }
.moon-crater:nth-child(4) { width: 2px; height: 2px; top: 5px; left: 8px; }
/* Ripple Effect */
.ripple {
position: fixed;
border-radius: 50%;
background: var(--accent);
opacity: 0.3;
pointer-events: none;
transform: scale(0);
z-index: 9999;
}
.ripple.animate {
animation: ripple-expand 0.8s ease-out forwards;
}
@keyframes ripple-expand {
0% {
transform: scale(0);
opacity: 0.4;
}
100% {
transform: scale(1);
opacity: 0;
}
}
/* Page Content Styles */
.content {
position: relative;
z-index: 1;
}
.card {
background: var(--card);
border: 1px solid var(--border);
border-radius: 16px;
transition: background-color 0.4s ease, border-color 0.4s ease, transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
transform: translateY(-4px);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.1);
}
.code-block {
font-family: 'JetBrains Mono', monospace;
background: var(--bg-secondary);
border: 1px solid var(--border);
border-radius: 8px;
padding: 1rem;
font-size: 0.875rem;
color: var(--fg-muted);
transition: background-color 0.4s ease, border-color 0.4s ease;
}
.btn-primary {
background: var(--accent);
color: #ffffff;
padding: 0.75rem 1.5rem;
border-radius: 8px;
font-weight: 500;
border: none;
cursor: pointer;
transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 8px 24px var(--accent-glow);
}
.btn-primary:active {
transform: translateY(0);
}
/* Staggered Animation */
.animate-in {
opacity: 0;
transform: translateY(20px);
}
.animate-in.visible {
animation: slideUp 0.6s ease forwards;
}
@keyframes slideUp {
to {
opacity: 1;
transform: translateY(0);
}
}
/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div class="bg-pattern"></div>
<div class="grid-overlay"></div>
<div class="content min-h-screen">
<!-- Header -->
<header class="flex items-center justify-between px-6 py-4 md:px-12 md:py-6">
<div class="flex items-center gap-3 animate-in" style="animation-delay: 0.1s;">
<div class="w-10 h-10 rounded-xl flex items-center justify-center" style="background: var(--accent);">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#ffffff" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/>
</svg>
</div>
<span class="text-xl font-bold" style="color: var(--fg);">Lumina</span>
</div>
<button
class="theme-toggle animate-in"
style="animation-delay: 0.2s;"
id="themeToggle"
aria-label="Toggle dark mode"
role="switch"
aria-checked="false"
>
<div class="toggle-track">
<div class="toggle-stars">
<div class="star"></div>
<div class="star"></div>
<div class="star"></div>
<div class="star"></div>
<div class="star"></div>
</div>
</div>
<div class="toggle-thumb">
<div class="toggle-icon">
<!-- Sun -->
<div class="sun-icon">
<div class="sun-center"></div>
<div class="sun-ray"></div>
<div class="sun-ray"></div>
<div class="sun-ray"></div>
<div class="sun-ray"></div>
<div class="sun-ray"></div>
<div class="sun-ray"></div>
<div class="sun-ray"></div>
<div class="sun-ray"></div>
</div>
<!-- Moon -->
<div class="moon-icon">
<div class="moon-body">
<div class="moon-crater"></div>
<div class="moon-crater"></div>
<div class="moon-crater"></div>
</div>
</div>
</div>
</div>
</button>
</header>
<!-- Hero Section -->
<main class="px-6 py-12 md:px-12 md:py-20 max-w-5xl mx-auto">
<div class="text-center mb-16">
<p class="text-sm font-medium tracking-widest uppercase mb-4 animate-in" style="color: var(--accent); animation-delay: 0.3s;">
Theme System
</p>
<h1 class="text-4xl md:text-6xl font-bold mb-6 animate-in" style="color: var(--fg); animation-delay: 0.4s;">
Switch your visual experience
</h1>
<p class="text-lg md:text-xl max-w-2xl mx-auto animate-in" style="color: var(--fg-muted); animation-delay: 0.5s;">
A beautifully crafted theme toggle with smooth transitions, localStorage persistence, and system preference detection.
</p>
</div>
<!-- Demo Cards -->
<div class="grid md:grid-cols-2 gap-6 mb-12">
<div class="card p-6 animate-in" style="animation-delay: 0.6s;">
<div class="flex items-center gap-3 mb-4">
<div class="w-8 h-8 rounded-lg flex items-center justify-center" style="background: var(--accent-glow);">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="var(--accent)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
</svg>
</div>
<h3 class="font-bold" style="color: var(--fg);">Dark Mode</h3>
</div>
<p style="color: var(--fg-muted);">
Easy on the eyes with carefully selected contrast ratios for optimal readability in low-light environments.
</p>
</div>
<div class="card p-6 animate-in" style="animation-delay: 0.7s;">
<div class="flex items-center gap-3 mb-4">
<div class="w-8 h-8 rounded-lg flex items-center justify-center" style="background: var(--accent-glow);">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="var(--accent)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="12" cy="12" r="5"/>
<line x1="12" y1="1" x2="12" y2="3"/>
<line x1="12" y1="21" x2="12" y2="23"/>
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/>
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/>
<line x1="1" y1="12" x2="3" y2="12"/>
<line x1="21" y1="12" x2="23" y2="12"/>
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/>
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/>
</svg>
</div>
<h3 class="font-bold" style="color: var(--fg);">Light Mode</h3>
</div>
<p style="color: var(--fg-muted);">
Clean and bright with warm tones that reduce blue light exposure during daytime use.
</p>
</div>
</div>
<!-- Code Preview -->
<div class="card p-6 animate-in" style="animation-delay: 0.8s;">
<h3 class="font-bold mb-4" style="color: var(--fg);">Quick Implementation</h3>
<div class="code-block overflow-x-auto">
<pre><code><span style="color: var(--accent);">// Check stored preference or system default</span>
const stored = localStorage.getItem('theme');
const system = window.matchMedia('(prefers-color-scheme: dark)').matches;
const theme = stored || (system ? 'dark' : 'light');
<span style="color: var(--accent);">// Apply theme</span>
document.documentElement.setAttribute('data-theme', theme);</code></pre>
</div>
</div>
<!-- CTA -->
<div class="text-center mt-12 animate-in" style="animation-delay: 0.9s;">
<button class="btn-primary">
Get Started
</button>
<p class="mt-4 text-sm" style="color: var(--fg-muted);">
Try the toggle in the top-right corner
</p>
</div>
</main>
</div>
<script>
// Initialize variables first
const themeToggle = document.getElementById('themeToggle');
const html = document.documentElement;
const STORAGE_KEY = 'theme-preference';
// Get stored theme or system preference
function getInitialTheme() {
const stored = localStorage.getItem(STORAGE_KEY);
if (stored) return stored;
return window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light';
}
// Apply theme to document
function applyTheme(theme, animate = false) {
if (animate) {
// Create ripple effect from toggle position
const rect = themeToggle.getBoundingClientRect();
const ripple = document.createElement('div');
ripple.className = 'ripple';
const size = Math.max(window.innerWidth, window.innerHeight) * 2;
ripple.style.width = size + 'px';
ripple.style.height = size + 'px';
ripple.style.left = (rect.left + rect.width / 2 - size / 2) + 'px';
ripple.style.top = (rect.top + rect.height / 2 - size / 2) + 'px';
document.body.appendChild(ripple);
// Force reflow
ripple.offsetHeight;
ripple.classList.add('animate');
setTimeout(() => ripple.remove(), 800);
}
html.setAttribute('data-theme', theme);
themeToggle.setAttribute('aria-checked', theme === 'dark');
localStorage.setItem(STORAGE_KEY, theme);
}
// Initialize theme on load
const initialTheme = getInitialTheme();
applyTheme(initialTheme);
// Toggle handler
themeToggle.addEventListener('click', () => {
const current = html.getAttribute('data-theme');
const next = current === 'dark' ? 'light' : 'dark';
applyTheme(next, true);
});
// Keyboard support
themeToggle.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
themeToggle.click();
}
});
// Listen for system preference changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (!localStorage.getItem(STORAGE_KEY)) {
applyTheme(e.matches ? 'dark' : 'light');
}
});
// Animate elements on scroll
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
}
});
}, { threshold: 0.1 });
document.querySelectorAll('.animate-in').forEach(el => observer.observe(el));
</script>
</body>
</html>