You Can Give A Website Dark Mode With Just One CSS Property

You Can Give A Website Dark Mode With Just One CSS Property was first published on Farai’s Codelab.

I’m too lazy to do all the work required to make a proper dark mode for now so I’m trying to find a quick fix. I tried to use filter: invert(100%) o…


This content originally appeared on DEV Community and was authored by Farai Gandiya

You Can Give A Website Dark Mode With Just One CSS Property was first published on Farai's Codelab.

I’m too lazy to do all the work required to make a proper dark mode for now so I’m trying to find a quick fix. I tried to use filter: invert(100%) on the body but it would literally invert everything, including images and emojis. I was about to give up on it before I came across Jim Nielsen’s post on the color-scheme property which tells the browser what color schemes a site (or element) supports and changes the default color based on the user’s preferences. To use it, you just need to specify the color-scheme property on the :root element.

:root {
    color-scheme: light dark;
}

This supports both light and dark mode. You can also just set light or dark as well as apply the color-scheme property to a specific element. In some environments (like email), you can also apply a <meta name="color-scheme"> tag.

Note that it doesn’t work in Firefox yet.

Issues

When I tried this the link colors didn’t change in Safari resulting in this ugly and inaccessible style. It worked perfectly fine in Chromium browsers however.

The site’s header with the logo and navigation links in full blue against a black background

Jim had the same issue. Because of that, I ended up implementing some sort of dark mode using Apple’s dynamic system colors. The CSS Colors Module 4 has system colors corresponding to the OS’s UI but they don’t have good support so I just copy-pasted the RGB values. Temper Temper explains how to “fix” dark mode on Safari better than I have.

Also, looking at color scheme’s spec, there was this scary warning noting that they don’t guarantee a color palette.

The light and dark color schemes don’t represent an exact color palette (such as black-and-white), but a range of possible palettes. To guarantee specific colors, authors must specify those colors themselves. Note also that, consequently, pairing default or colors with author-specified colors cannot guarantee any particular contrast level; it might be necessary to set both foreground and background colors together to ensure legibility.

Given those issues my attempt to implement dark mode lazily has become the start of an interesting journey. Still, it’s worth putting in.

Additional Reading

Thanks for reading! If you liked this post, consider supporting my work by:

You can also subscribe to my newsletter.

Have feedback? Send an email to gandiyafarai+feedback at gmail dot com


This content originally appeared on DEV Community and was authored by Farai Gandiya


Print Share Comment Cite Upload Translate Updates
APA

Farai Gandiya | Sciencx (2021-06-27T10:02:51+00:00) You Can Give A Website Dark Mode With Just One CSS Property. Retrieved from https://www.scien.cx/2021/06/27/you-can-give-a-website-dark-mode-with-just-one-css-property/

MLA
" » You Can Give A Website Dark Mode With Just One CSS Property." Farai Gandiya | Sciencx - Sunday June 27, 2021, https://www.scien.cx/2021/06/27/you-can-give-a-website-dark-mode-with-just-one-css-property/
HARVARD
Farai Gandiya | Sciencx Sunday June 27, 2021 » You Can Give A Website Dark Mode With Just One CSS Property., viewed ,<https://www.scien.cx/2021/06/27/you-can-give-a-website-dark-mode-with-just-one-css-property/>
VANCOUVER
Farai Gandiya | Sciencx - » You Can Give A Website Dark Mode With Just One CSS Property. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/27/you-can-give-a-website-dark-mode-with-just-one-css-property/
CHICAGO
" » You Can Give A Website Dark Mode With Just One CSS Property." Farai Gandiya | Sciencx - Accessed . https://www.scien.cx/2021/06/27/you-can-give-a-website-dark-mode-with-just-one-css-property/
IEEE
" » You Can Give A Website Dark Mode With Just One CSS Property." Farai Gandiya | Sciencx [Online]. Available: https://www.scien.cx/2021/06/27/you-can-give-a-website-dark-mode-with-just-one-css-property/. [Accessed: ]
rf:citation
» You Can Give A Website Dark Mode With Just One CSS Property | Farai Gandiya | Sciencx | https://www.scien.cx/2021/06/27/you-can-give-a-website-dark-mode-with-just-one-css-property/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.