Halloween Sale 15% off Ends in See the plans
HTML

Dark Mode Toggle Button

Adds a floating moon button that switches the site to a dark color scheme, remembers the visitor's choice and follows their system setting on the first visit.

Don’t have WPIDE yet? Install it on your site

Code

<button type="button" id="wpide-dark-toggle" aria-label="Toggle dark mode" aria-pressed="false">☾</button>
<style>
#wpide-dark-toggle{position:fixed;left:20px;bottom:20px;z-index:9999;width:42px;height:42px;border:0;border-radius:50%;background:#1e1e1e;color:#fff;font-size:20px;line-height:42px;cursor:pointer;box-shadow:0 2px 8px rgba(0,0,0,.25);}
html.wpide-dark{filter:invert(1) hue-rotate(180deg);background:#fff;}
html.wpide-dark img,html.wpide-dark video,html.wpide-dark iframe,html.wpide-dark picture,html.wpide-dark [style*="background-image"],html.wpide-dark #wpide-dark-toggle{filter:invert(1) hue-rotate(180deg);}
</style>
<script>
(function () {
    var root = document.documentElement, button = document.getElementById('wpide-dark-toggle');
    var stored = null;
    try { stored = localStorage.getItem('wpide-dark'); } catch (e) {}
    var dark = stored !== null ? stored === '1' : (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
    function apply() {
        root.classList.toggle('wpide-dark', dark);
        button.setAttribute('aria-pressed', dark ? 'true' : 'false');
        button.innerHTML = dark ? '☀' : '☾';
    }
    button.addEventListener('click', function () {
        dark = !dark;
        try { localStorage.setItem('wpide-dark', dark ? '1' : '0'); } catch (e) {}
        apply();
    });
    apply();
})();
</script>

Notes

This uses a color inversion that works on most themes; photos and videos are re-inverted so they look normal. Tune the colors if your theme has its own dark palette.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.