3 ms·
Dark Mode in CSS Guide
- bruno-366 2y agohaha, I was looking at this guide yesterday :) I can also recommend a color palette generator (especially one like the one linked below that auto-generates a dark-mode version too). That way you can use css variable for your colors and then using the media query @media (prefers-color-scheme: dark) your site can auto update the colors to darkmode. https://www.hover.dev/css-color-palette-generator https://www.hover.dev/css-color-palette-generator
- jonkoops 2y agoStoring the user preference in local storage opens up a whole can of worms. What if the user has multiple tabs/windows open? Now you should probably be using the 'storage' event to synchronize the state (which is not done in this article). Otherwise, prepare to be blasted with a sea of white when switching tabs. What if the user toggles their preference on a system level? Should this then override the locally stored value? Should it be cleared? Or is the stored preference persisted over the system preference at all times, leaving the user unable to reset the value to restore the behavior of following system preferences first. Additionally, you will now have to add JavaScript in a critical page load path to add classes to avoid a flash of the wrong color. As well as critical CSS to ensure the most basic colors are applied early on while the rest of your stylesheet loads in. My take on this is, don't even try to add a dark mode toggle to the site itself, always rely on CSS and never JS. This is the responsibility of the browser, you can even customize the appearance in your browser settings separately of the operating system. It will load user agent styles with dark mode if the 'color-scheme' meta is set correctly, so you don't have to even do the critical styles to avoid flashes. I would really like to see Firefox, Chrome, etc. add a dark mode toggle in the navigation bar that could simply be used to toggle the 'prefers-color-scheme' value on sites that have the 'color-scheme' meta set up correctly, so we don't have to constantly implement this feature over and over again on every site (usually done wrong).
- atmanactive 2y agoAbsolutely agree. It's the browser's job to respect and maintain my OS-level dark mode. Nothing else will ever be able to stop the dreaded full-screen-white-flash-while-loading. On OLEDS and AR/VR this should bear "epilepsy warning" as these flashes of white screen are pure torture.
- crazygringo 2y agoBut from the article: > But what if users want to override their system preference for a site? Just because a user prefers dark mode for their OS doesn’t always mean they prefer it on a website. That’s why providing a way to manually override dark mode, despite the system settings, is a good idea. And I agree. You want to follow the OS, but be able to override per-site. I like dark mode at night but it strains my eyes to read longform text in it. So I want news sites and Wikipedia only to stay in light mode, and I turn my brightness way down while I'm reading them.
- atmanactive 2y agoYes, sure, I agree, but even then, what is the harm of a BLACK page flashing for a moment while the site is loading? None. In cinema, for example, fade-to-black is ordinary but purely white scenes are extremely rare.
- palsecam 2y agoTwo interesting and rarely considered points in that guide: ① “Decrease the brightness and contrast of images a bit so that it looks comfortable to the eyes when it’s against a dark background.” ② “If we simply invert a dark shadow using light colors, then we get this funky thing […] Close elements should still be lighter and distant elements should still be darker – even in a dark UI.” Oh yes, shadows are tricky. I got ① right for the dark theme of https://FreeSolitaire.win https://FreeSolitaire.win, but I never thought of ②. Good to know!
- account42 2y ago> ① “Decrease the brightness and contrast of images a bit so that it looks comfortable to the eyes when it’s against a dark background.” Please don't do this. It just makes dark mode into an inferior version with limited contrast/color depth instead of a real alternative.
- phatskat 2y agoI definitely get that - I think in settings where that richness and depth is a priority it makes sense to have images at their original color and brightness. However, charts and general imagery is probably fine to tune. An alternative would be something like hover/click/tap to show the full-brightness image, but that’s something of a discoverability issue unless there’s clear direction given to the user, which doesn’t fit a lot of use cases cleanly imo. A better alternative would be something configured in the browser that passes the user’s preference to the page, and an API similar to color scheme that controls image brightness in different theme modes. This is probably the best for users and developers, and least likely to be a “thing”.
- 752963e64 2y ago[dead]
- dankobgd 2y ago[flagged]
- deleted 2y ago[deleted]
- deleted 2y ago[deleted]
- account42 2y ago> Toggling Themes > There several approaches to go about doing this: > Using a Body Class > Using Separate Stylesheets > Using Custom Properties > Using Server-Side Scripts I wish browsers would just expose a more visible prefers-color-scheme toggle. Adding this to every website separately makes no sense. It would also make it more likely that websites respect my configured preference. Not related to websites, but I would also like to take this upportunity to remind everyone that dark/like toggles might be a win over a single fixed color scheme but are still a massive downgrade over the fully customizable color schmes that desktop operating systems used to support.
- Y-bar 2y agoI think it is a great feature for add-ons and extensions to add additional user features such as this. You can find a lot of extensions such as https://addons.mozilla.org/en-US/firefox/addon/dark-mode-webextension/ https://addons.mozilla.org/en-US/firefox/addon/dark-mode-web...
- phatskat 2y agoExtensions are fine, and also something like this that’s part of the standards used on the web makes a good case for an in-browser toggle that’s on the main UI.