Open shreshthmohan opened 2 years ago
Dark mode survey: dev channel on medium
What I want to achieve:
prefers-color-scheme
media query)Seems like it would be challenging to achieve this with Tailwind CSS.
Disable JS on overreacted.io and swyx.io and change your OS color scheme setting. The theme on these sites won't change.
Fix: Listen to changes to prefers-color-scheme
. Wait but this won't work when JS is disabled.
url: https://shreshth.dev/blog/dark-mode-that-just-works slug: dark-mode
We, as developers who blog, don't need JavaScript to support dark mode!
In fact, if you use JS to make dark mode work, it most likely won't work for users who have it disabled.
I will attempt to convince you that you don't need any JS and minimal CSS to get dark mode to work for your blog site. And I shall show you how to do it.
Use
color-scheme
meta tag or CSS propertycolor-scheme
will get your site to look decent by making use of the browser's defaults for dark mode.You can use either the meta tag[^1] or the CSS property[^2], but prefer the meta tag. Reason stated below.
Meta tag
One would prefer the use of this meta tag instead of CSS to prevent a flash of unstyled content (FOUC):
Demo for using the
color-scheme
meta tagCSS property
Demo for using the
color-scheme
CSS propertycolor-scheme
is that you don't have to write any extra styles to make things readable. The user-agent (browser) stylesheet will take care of it. So you can support dark mode with little to no extra CSS.Use the
prefers-color-scheme
media query to override user-agent stylesNow if you don't like the browser's default styles for dark mode, you can override them using the
prefers-color-scheme
media query[^4].Having already set the
color-scheme
you will only need to worry about those elements whose user-agent stylesheet you don't particularly like.For example, Chrome sets black as the background and white as the default text color, but designers don't recommend using pure black and white. GitHub's dark mode sets
#c9d1d9
as the text color and#0d1117
as the background color in dark mode. These are easier on the eyes than browser defaults.Demo for using
color-scheme
meta tag withprefers-color-scheme
media queryUse some JS if you really want (still an unsatisfactory solution)
Now, some of you might be itching to add some JS.
Well, in my opinion, you don't really need it, but one can argue that the user might find it time-consuming to go into their OS settings to change the appearance setting.
To solve that, you can give them a button to quickly change the dark mode setting directly on your site.
I would do the following.
color-scheme
meta tag andprefers-color-scheme
media query as mentioned above.demo for manually overriding OS appearance setting with JS But using JS still poses problems with elements you haven't explicitly overridden styles for, say, scrollbars on overflowing content. You may not even be able to solve that entirely because of the lack of CSS properties to style several elements (e.g. scrollbars)
Conclusion
In conclusion, it's best to use the
color-scheme
meta tag withprefers-color-scheme
media query to support dark mode properly. Using JS is mostly going to worsen the user experience.Demo for using
color-scheme
meta tag withprefers-color-scheme
media query[^1]:
color-scheme
meta tag in html spec [^2]:color-scheme
CSS property on MDN [^3]:color-scheme
meta tag section from the web.dev article on Improved dark mode default styling [^4]:prefers-color-scheme
media query on MDN