DEV Community

Discussion on: One line - Dark Mode using CSS

Collapse
 
jojobyte profile image
jojobyte • Edited

Someone likely mentioned this already, but there seems to be a "gotcha" with Firefox and Edge.

Firefox appears to not apply the filter to the background-color of the html or body element.

And Edge (not that I actually care) wont apply the filter to body but will to html

So these wont work

html[theme='dark-mode'] {
    filter: invert(1) hue-rotate(180deg);
}

html { background-color: #fff; } /* no luck in firefox, works in Edge */
body { background-color: #fff; } /* no luck in firefox nor in Edge */
Enter fullscreen mode Exit fullscreen mode

Cross-Browser Workaround

Put all your stuff in a wrapper element just under the body.

html[theme='dark-mode'] {
    filter: invert(1) hue-rotate(180deg);
}

/* create a wrapper element and it works great */
body > .wrapper { background-color: #fff; }
Enter fullscreen mode Exit fullscreen mode