HTML

Creating Dark Mode, for Lazy People

Sponsor

A lot of people spend a lot of time wondering how to create a light mode version of their sites. Stop wasting your time! Just invert your html tag. Note: may lead to unexpected colors.

/* Fjolt is dark by default, but you can change the class names */ body.lightmode { filter: invert(1); } /* Firefox requires this line: */ html.lightmode body { background: #efe9df; }
if(document.getElementById('light-dark-mode') !== null) { document.getElementById('light-dark-mode').addEventListener('pointerdown', function(e) { let root = document.getElementsByTagName( 'html' )[0] if(root.classList.contains('lightmode')) { root.classList.remove('lightmode'); } else { root.classList.add('lightmode'); } }); }

Joking aside, this does kind of work. Interestingly, Chrome and Firefox both seem to invert borders first. In Firefox, the background will not invert on the body tag, implying the filter is applied to the body content, not the html document tag. This little demo demonstrates quite a lot about how different rendering engines work.

And if you're careful with your colors, you might be able to make this work somewhere.

Enable Light Mode:

Turn it on!

Support for filters

Data on support for the css-filters feature across the major browsers from caniuse.com

Last Updated Saturday, 13 March 2021
Johnny Simpson
Johnny Simpson

More Tips and Tricks HTML

Subscribe for Weekly Dev Tips

Subscribe to our weekly newsletter, to stay up to date with our latest web development and software engineering posts via email. You can opt out at any time.

Not a valid email