JavaScript / HTML / CSS JavaScript Free

JavaScript: Dark Mode Toggle Saved in localStorage

Add a dark mode toggle that remembers the visitor's choice in localStorage — and respects their system setting first.

22 lines · Updated Nov 26, 2026

Preview of JavaScript: Dark Mode Toggle Saved in localStorage
javascript-dark-mode-toggle.js
<!-- <button id="theme-toggle">Dark mode</button> -->
<script>
(function () {
    var saved = localStorage.getItem('theme');
    var prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
    var theme = saved || (prefersDark ? 'dark' : 'light');

    document.documentElement.setAttribute('data-theme', theme);

    document.getElementById('theme-toggle').addEventListener('click', function () {
        var current = document.documentElement.getAttribute('data-theme');
        var next = current === 'dark' ? 'light' : 'dark';
        document.documentElement.setAttribute('data-theme', next);
        localStorage.setItem('theme', next);
        this.textContent = next === 'dark' ? 'Light mode' : 'Dark mode';
    });
})();
</script>

<!-- <style>
  [data-theme="dark"] { background: #111; color: #eee; }
</style> -->

About this snippet

Many visitors browse with their device set to dark mode, especially at night, and they expect your site to respect it — and to remember their choice. This snippet adds a dark mode toggle with plain JavaScript that follows the visitor's system preference by default, lets them switch with a button, and saves the choice in localStorage so it persists across pages and visits.

How it works

  1. On page load it checks for a saved choice. If there is none, it reads the system preference with matchMedia('(prefers-color-scheme: dark)').
  2. It sets a data-theme attribute on the <html> element.
  3. Clicking the toggle switches between light and dark, updates the attribute, saves the choice and updates the button text.
  4. Your CSS uses [data-theme="dark"] selectors to change colours.

How to use it

  • Define your colours as CSS custom properties (--bg, --text) and override them in the dark theme — much easier than restyling every element.
  • Put the theme-setting part of the script in the <head> so the correct theme is applied before the page paints.

Avoid the "flash" of the wrong theme

If the script runs at the end of the page, dark-mode users briefly see the light theme. A tiny inline script in the <head> that only sets the attribute fixes this.

Design tips

  • Use dark grey (for example #111–#1a1a1a) rather than pure black, and off-white text.
  • Check contrast for links and buttons in both themes.
  • Images with white backgrounds may need slightly reduced brightness in dark mode.

FAQ

Does localStorage need a cookie banner? Storing a purely functional preference like a theme is generally considered essential, but check the rules that apply to your site.

Related: Copy to clipboard button · Reading-progress bar

How to install

Add the button and script to your page, then style [data-theme="dark"] in your CSS. The choice is saved in localStorage, so it survives reloads.

Share this snippet

See all
Chat with us