Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying webpage in dark mode #5102

Closed
ajtruckle opened this issue Feb 13, 2025 · 4 comments
Closed

Displaying webpage in dark mode #5102

ajtruckle opened this issue Feb 13, 2025 · 4 comments

Comments

@ajtruckle
Copy link

I notice in Microsoft Edge it has a toggle to attempt to display the loaded page in dark mode.

I appreciate that nothing can be better than a dedicated dark colour scheme using CSS, but is it possible to toggle this same setting for a page using WebView2?

@sivMSFT
Copy link

sivMSFT commented Feb 14, 2025

Hi @ajtruckle,

Just to clarify, are you looking for a feature that includes a toggle button for wv2 users to switch between Light and Dark Mode? Also, are you inquiring whether this feature is currently live?

Thank you!

@ajtruckle
Copy link
Author

Hi @sivMSFT

  1. I refer to this toggle. I understand it renders page in dark mode?

Image

  1. Yes, is this option available?

@sivMSFT
Copy link

sivMSFT commented Feb 18, 2025

Hi @ajtruckle, you can use the CoreWebView2Profile.PreferredColorScheme property to set the dark mode. This property allows you to choose between dark mode, light mode, or the OS default, and it should apply to all the WebView2 owned UI.

You can also enable Auto Dark Mode through AdditionalBrowserArguments:
new CoreWebView2EnvironmentOptions() { AdditionalBrowserArguments = "--enable-features=WebContentsForceDark" };

@ajtruckle
Copy link
Author

@sivMSFT
Thanks, the first suggestion was useful because now my browser control reflects my application theme setting. Thank you. 🙏

But, as for the other aspect of this, it looks like I need to use media queries. For example:

/* Dark mode for screens only */
@media screen and (prefers-color-scheme: dark) {
    body {
        background-color: black;
        color: white;
    }
}

I have tested in Edge so assume that WebView2 will also work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants