How to add dark mode to your site — in 4 steps (no manual repainting)

"Dark mode" sounds like brutal work: go through every button, every block, and repaint them by hand. Good news — nobody does it that way. The trick is that colors are defined once through variables, and the toggle just swaps their values all at once. Below are four steps that leave you with a theme you switch with one button. Plus a ready prompt so AI builds it for you.
Step 1. Move colors into variables
Stop writing colors straight into styles (color: #fff). Collect them in one place — CSS variables on the page root:
:root {
--bg: #ffffff;
--text: #141416;
}
body {
background: var(--bg);
color: var(--text);
}
Now the whole site pulls color from two variables instead of a hundred hardcoded values. That's the entire prep.
Step 2. Describe the dark set behind one switch
Add a second set of the same variables — it kicks in when the page has the attribute data-theme="dark":
[data-theme="dark"] {
--bg: #141416;
--text: #f4f4f4;
}
Nothing else to repaint. Change the attribute — the variables change — the whole site recolors itself. That's where all the magic lives.
Step 3. Wire up a toggle button
The button needs one action: set or remove that attribute on <html>.
<button id="theme">🌙</button>
<script>
document.getElementById('theme').onclick = () => {
const el = document.documentElement;
const dark = el.getAttribute('data-theme') === 'dark';
el.setAttribute('data-theme', dark ? 'light' : 'dark');
};
</script>
Press the button — the theme flips. That's a working minimum already.
Step 4. Remember the choice and respect the system
Two small things separate a toy from a real theme:
- Remember the choice so the theme doesn't reset after a reload — save it to
localStorage. - Respect the system theme — if someone's phone is in dark mode, show dark right away.
// on click — save the choice:
localStorage.setItem('theme', dark ? 'light' : 'dark');
// on page load — restore it:
const saved = localStorage.getItem('theme');
const prefersDark = matchMedia('(prefers-color-scheme: dark)').matches;
document.documentElement.setAttribute('data-theme', saved ?? (prefersDark ? 'dark' : 'light'));
What you get
A button that instantly flips light and dark across the whole site, remembers the choice between visits, and matches the person's system setting on the first load. And all of it rides on two sets of variables — not a single element repainted by hand.
Ask AI — but the right way
This is a perfect task to hand to AI. Just don't ask "make dark mode" — you'll get mush. Give it the plan:
Add dark mode to my siteThe difference is that the second prompt sets the architecture — so AI doesn't invent its own crooked one. On phrasing tasks like this in general, see how to write a good prompt.
A common gotcha: images, shadows, and borders
Variables recolor the background and text, but a few things are easy to forget — and they give away a half-done job:
- Images and logos. A black logo vanishes on a dark background. Keep a light version for dark mode or give it a subtle backdrop.
- Shadows. A soft grey shadow, pretty on white, turns into a dirty smudge on dark. In dark mode shadows are usually made barely visible or dropped entirely.
- Borders and dividers. Light-grey lines disappear on dark. Give them their own
--bordervariable and override it in the dark set.
Same rule: any color that "gives away" the theme goes into a variable, not a hardcode.
Why does dark mode flash white on load?
The classic bug: the page shows the light theme for a blink, then switches. The cause is that the theme script sits at the bottom and runs late. Easy fix: set data-theme at the very top of <head>, before the page renders. And if styles don't apply at all, check why your CSS isn't working.
What if my site is on React?
Same idea — variables and an attribute on <html> — but the toggle usually hangs off component state. React is handy exactly here: you change the theme state and it sets the attribute. The CSS part (variables + [data-theme="dark"]) doesn't change at all.
Does this work on phones?
Yes — prefers-color-scheme reads the system theme on phones too. While you're at it, check the site itself fits a small screen — see how to make a site mobile-friendly.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





