DevPlatform
Home
Components
Search
Submit
Earn
Login
Sign Up Free
Dark Light Toggle - Preview | DevPlatform
DevPlatform
Categories
Accordion
Animation
Box Shadow
Button
Card
Footer
Form
Gallery
Hero Section
Inputs
Loader
Marquee
Modal
Modules
Navbar
Progress Bar
Radio Buttons
Search-Bar
Sections
Slider
Tab
Timeline
Toggle
Dark Light Toggle
jitesh208
Jan 28, 2026
2
0
Share
Live Preview
Refresh
Live Editor
HTML
CSS
JS
HTML
CSS
JavaScript
1
<div class="toggle-card"> <div class="card-header"> <div class="card-title"> <div class="title-icon">🌓</div> <h2>Theme Settings</h2> </div> <label class="theme-switch" for="theme-toggle" aria-label="Toggle dark mode"> <input type="checkbox" id="theme-toggle"> <span class="slider"> <span class="slider-icon sun-icon">☀️</span> <span class="slider-icon moon-icon">🌙</span> </span> </label> </div> <p class="card-content"> Toggle between light and dark mode for a comfortable viewing experience. Your preference will be saved automatically. </p> <div class="status-badge"> <span class="status-dot"></span> <span id="status-text">Light Mode Active</span> </div> </div>
1
:root { --bg-light: #f8fafc; --bg-dark: #0f172a; --card-light: #ffffff; --card-dark: #1e293b; --text-light: #1e293b; --text-dark: #f1f5f9; --border-light: #e2e8f0; --border-dark: #334155; --accent: #3b82f6; --accent-hover: #2563eb; } * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif; min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg-light); transition: background 0.3s ease; padding: 20px; } body.dark-mode { background: var(--bg-dark); } /* Toggle Card */ .toggle-card { background: var(--card-light); border: 1px solid var(--border-light); border-radius: 16px; padding: 32px; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05), 0 10px 25px rgba(0, 0, 0, 0.05); transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); max-width: 400px; width: 100%; } body.dark-mode .toggle-card { background: var(--card-dark); border-color: var(--border-dark); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3), 0 10px 25px rgba(0, 0, 0, 0.2); } .toggle-card:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08), 0 15px 35px rgba(0, 0, 0, 0.08); } body.dark-mode .toggle-card:hover { box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4), 0 15px 35px rgba(0, 0, 0, 0.3); } .card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; } .card-title { display: flex; align-items: center; gap: 12px; } .title-icon { width: 40px; height: 40px; background: linear-gradient(135deg, #3b82f6, #8b5cf6); border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 1.3em; } .card-title h2 { font-size: 1.35em; font-weight: 600; color: var(--text-light); transition: color 0.3s ease; } body.dark-mode .card-title h2 { color: var(--text-dark); } /* Toggle Switch */ .theme-switch { position: relative; display: inline-block; width: 60px; height: 32px; } .theme-switch input { opacity: 0; width: 0; height: 0; } .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background: #cbd5e1; transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); border-radius: 34px; box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1); } body.dark-mode .slider { background: #475569; } .slider:before { position: absolute; content: ""; height: 24px; width: 24px; left: 4px; bottom: 4px; background: white; transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); border-radius: 50%; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2); } input:checked + .slider { background: var(--accent); } input:checked + .slider:before { transform: translateX(28px); } input:focus + .slider { box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1), 0 0 0 3px rgba(59, 130, 246, 0.2); } .slider:hover { background: #94a3b8; } body.dark-mode .slider:hover { background: #64748b; } input:checked + .slider:hover { background: var(--accent-hover); } /* Icon inside slider */ .slider-icon { position: absolute; top: 50%; transform: translateY(-50%); font-size: 15px; transition: opacity 0.3s ease; } .sun-icon { left: 8px; opacity: 1; } .moon-icon { right: 8px; opacity: 0; } input:checked ~ .sun-icon { opacity: 0; } input:checked ~ .moon-icon { opacity: 1; } /* Card Content */ .card-content { color: #64748b; line-height: 1.6; font-size: 0.95em; margin-bottom: 20px; transition: color 0.3s ease; } body.dark-mode .card-content { color: #94a3b8; } /* Status Badge */ .status-badge { display: inline-flex; align-items: center; gap: 8px; padding: 8px 14px; background: #f1f5f9; border-radius: 8px; font-size: 0.9em; font-weight: 500; color: var(--text-light); transition: all 0.3s ease; } body.dark-mode .status-badge { background: #334155; color: var(--text-dark); } .status-dot { width: 8px; height: 8px; background: var(--accent); border-radius: 50%; animation: pulse 2s ease-in-out infinite; } @keyframes pulse { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.6; transform: scale(1.1); } } /* Responsive */ @media (max-width: 480px) { .toggle-card { padding: 24px; } .card-header { flex-direction: column; gap: 16px; align-items: flex-start; } .card-title h2 { font-size: 1.2em; } } /* Accessibility */ @media (prefers-reduced-motion: reduce) { * { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; } }
1
const toggle = document.getElementById('theme-toggle'); const body = document.body; const statusText = document.getElementById('status-text'); // Check for saved theme preference or default to light mode const currentTheme = localStorage.getItem('theme') || 'light'; if (currentTheme === 'dark') { body.classList.add('dark-mode'); toggle.checked = true; statusText.textContent = 'Dark Mode Active'; } // Toggle theme and save preference toggle.addEventListener('change', function() { if (this.checked) { body.classList.add('dark-mode'); localStorage.setItem('theme', 'dark'); statusText.textContent = 'Dark Mode Active'; } else { body.classList.remove('dark-mode'); localStorage.setItem('theme', 'light'); statusText.textContent = 'Light Mode Active'; } });
Comments
0
Want to comment?
Please
login
or
sign up
to post comments.
No comments yet
Be the first to comment on this component!
Comments 0
Want to comment?
Please login or sign up to post comments.
No comments yet
Be the first to comment on this component!