admin管理员组

文章数量:1287489

I'm building an app where I display emails on it. I have no control over those emails, so I don't know what their CSS looks like.

Is there any "easy" way to automatically switch the whole HTML to dark mode, whenever possible?

Just like Gmail does when your device has Dark Mode enabled.

I'm using React Native and Expo, displaying the HTML through WebView, in case it's of any help. Thanks!

I'm building an app where I display emails on it. I have no control over those emails, so I don't know what their CSS looks like.

Is there any "easy" way to automatically switch the whole HTML to dark mode, whenever possible?

Just like Gmail does when your device has Dark Mode enabled.

I'm using React Native and Expo, displaying the HTML through WebView, in case it's of any help. Thanks!

Share Improve this question asked Oct 8, 2020 at 5:28 Franco MuñizFranco Muñiz 9211 gold badge10 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

I personally tend to respect the email's CSS and keep them as-is.

There's no easy way to get perfect result, since the "dark mode" is not well defined concept, and depends on the actual CSS of specific content.

But a rough solution is quite cheap. Just apply the CSS filter: invert(100%) to whole HTML.

@media (prefers-color-scheme: dark) {
  html {
    filter: invert(100%);
  }

  /* double apply invert filter to cancel out effect on images */
  img {
    filter: invert(100%);
  }
}

Here's an easy way to switch back and forth between light and dark mode:

javascript:(function(){
    document.documentElement.style.filter = document.documentElement.style.filter ? '' : 'invert(100%)'
})();

Copy/paste in your browser on any website to test.

本文标签: javascriptConvert any HTML to Dark ModeStack Overflow