Blog / Build with AI /

Building a dark mode toggle with AI

Dark mode is one of those features that looks like a weekend job and turns into a week. The toggle itself is easy. The part that bites is the flash of white when the page loads, or the toggle that forgets what you chose, or the one image that glows like a lighthouse on a dark background.

The finished thing, in one line: a button that switches a page between light and dark, starts in whatever the visitor’s device already prefers, and remembers their choice next time.

This is a good job to hand to AI, because the pattern is well known and the code is short. It’s also a good job to understand, because the mistakes are all in the details.

Step 1: build it on CSS variables

The trick that makes dark mode painless is to stop writing colours directly into your styles. Define them once as variables, then use the variables everywhere.

:root {
  --bg: #ffffff;
  --text: #1a1a1a;
  --link: #0b5fff;
}

[data-theme="dark"] {
  --bg: #121212;
  --text: #eaeaea;
  --link: #7aa7ff;
}

body {
  background: var(--bg);
  color: var(--text);
}
a { color: var(--link); }

Switching themes then means changing one attribute on the <html> element, and every colour follows.

If you already have a stylesheet full of hard-coded colours, this is where AI earns its keep. Try:

“Here is my CSS. Replace every hard-coded colour with a CSS variable defined in :root, and add a [data-theme=’dark’] block with sensible dark equivalents. Keep the layout untouched and list every variable you created.”

Read the result. Check that the dark palette isn’t just pure black with pure white text, which tends to be harsh to read. A very dark grey with slightly softened text is gentler.

Step 2: respect the system setting

Browsers can tell you whether the visitor has asked their device for dark mode, using the prefers-color-scheme media query. Using it means the first visit already feels right.

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121212;
    --text: #eaeaea;
    --link: #7aa7ff;
  }
}

The :not([data-theme="light"]) part means: follow the system, unless the visitor has picked light themselves. It’s slightly clumsy because the dark colours now appear twice. If that bothers you, you can set the attribute from JavaScript instead, as in the next step.

Step 3: the toggle and the memory

A small script reads the saved choice, falls back to the system preference, applies it, and handles clicks.

<button id="theme-toggle" type="button" aria-pressed="false">Dark mode</button>

<script>
  const root = document.documentElement;
  const button = document.getElementById('theme-toggle');
  const saved = localStorage.getItem('theme');
  const systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
  const start = saved || (systemDark ? 'dark' : 'light');

  function apply(theme) {
    root.setAttribute('data-theme', theme);
    button.setAttribute('aria-pressed', theme === 'dark');
  }

  apply(start);

  button.addEventListener('click', () => {
    const next = root.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
    localStorage.setItem('theme', next);
    apply(next);
  });
</script>

Three things are going on. localStorage keeps the visitor’s choice in their own browser. matchMedia checks the system preference when nothing is saved. And aria-pressed tells screen readers whether the button is on or off.

Paste that into a test page before anything else. Click the button, refresh, and confirm it remembers.

Step 4: stop the white flash

This is the bug nearly every first attempt has. The page loads in light colours, then JavaScript runs and flips it to dark, and the visitor sees a bright flash.

The fix is to run the small “read the saved theme and set the attribute” part in the <head>, before the page paints:

<script>
  (function () {
    var saved = localStorage.getItem('theme');
    var dark = window.matchMedia('(prefers-color-scheme: dark)').matches;
    document.documentElement.setAttribute('data-theme', saved || (dark ? 'dark' : 'light'));
  })();
</script>

Keep it tiny and put it above your stylesheet links. The button’s click handler can stay at the bottom of the page.

If your AI-written version doesn’t do this, ask:

“My dark mode flashes light on page load. Move the theme detection into a small inline script in the head so the correct theme is applied before first paint.”

Step 5: check everything that isn’t text

The common leftovers:

  • Images with white backgrounds or dark logos that vanish on dark. Provide a light-on-dark version, or give the image a subtle background.
  • Form fields. Inputs often keep browser-default white. Style them with your variables too.
  • Borders and shadows. A soft grey border that looks neat on white can disappear, and shadows barely show on dark.
  • Contrast. Test both themes with a contrast checker. Dark mode isn’t automatically readable.
  • Embedded content like maps or videos, which won’t follow your theme.

You can also add color-scheme: light dark; to :root, which tells the browser to darken built-in elements like scrollbars and form controls.

What tends to go wrong

If you build this with AI, the likely failures are: the toggle works but the choice isn’t remembered, because the script saves to storage but never reads it back; colours still hard-coded in an inline style somewhere, so one block stays white; and a script that errors in private browsing, where storage can be blocked. Wrapping the storage calls in try/catch is a sensible habit.

When something misbehaves, give the AI the actual symptom (“the footer stays white in dark mode”) rather than asking it to “fix dark mode”. Specific complaints get specific fixes. It’s the same discipline I described in building a tiny tool with AI.

Where it matters for websites

If you manage a content site, dark mode is a nice-to-have for readers who browse at night, not a ranking factor. Treat it as comfort, not strategy. Add it if it’s cheap, and don’t let it break your layout or slow your page. It adds almost no weight when done this way.

Next step

Try it on a single static page first. Once it behaves, move the variables into your real stylesheet. If you’re working in WordPress, do it in a child theme so a theme update doesn’t wipe it out.

Which part of your site do you think would look worst in dark mode? That’s the one to test first.