Dark Mode is an extremely popular feature to implement into your website using basic HTML, CSS and JS. So why don't you have it on yours yet? In th...
For further actions, you may consider blocking this person and/or reporting abuse
👋 Interesting approach but why not leave theme detection to browser?
Why not both?
And do the same check in the post but for a string via a switch. This also opens up the ability for different themes (maybe a dark mode that's actually light enough to blend in with the browser UI in dark mode, an amoled dark mode, a solarized mode, a few color-blind modes)
This is also a great way to do it!
Instead of
onload="onload()"
, I would prefer this, since you would only need to add one line to your page (<script src='script.js'></script>
):But otherwise, this is great!
You should use event listeners if you want to be on the safe side
window.onload
can be overwritten andeventlistener
adds an listener to the page and will not be overwrittenGood point. However for this demo I wanted to keep it simple. For those who are into JS and have more than one function set for onload, they should understand how it works. But eventlistener is a great alternative.
Definitely works too! Thanks for the tip!
welcome , my pleasure!
Hey Matthew,
Yeah I agree with you. The dark/light mode toggle is a really popular feature and can really enhance a website.
Just from an accessibility point of view, If I was to have a toggle on my website I would first let it default to my OS dark/light mode.Then as that changes on the site you could consider then to use local storage.
We have a media query in CSS called
prefers-color-scheme
which we can access in JS with window.matchMedia.web.dev/prefers-color-scheme :
This blog is really worth a read. It even talks about only loading CSS we need and not loading non critical CSS. You can also see the power of custom properties.
I will definitely read it because the preferred color scheme is a great feature to implement as well! Thanks Gary!
Thanks it really helped🙌
Not sure what browser your in. All edges work for me.
Is there something wrong with just using the DarkReader extension?
Nothing wrong with that. And never said there was. This is just one way to achieve dark mode.
I just wonder why people are wasting time coding in dark themes when modern browsers have extensions to do that.
You have a good point
Actually mobile web pages still need it.
Great post!
I'm from HelloGitHub, a nonprofit organization in China. I am reaching out to you to ask if it's OK for us to translate your posts, "Implement Dark Mode On Your Website.", into Chinese and publish it on our WeChat official account. You own the copyright of course.
How does that sound to you?
As long as my Dev.to profile is linked and I'm credited that would be fine.
Nice use of dark mode,
I've made a dark mode site please check it out here
d8wbyo.mimo.run/index.html
You have to click on clock's time display area to switch between modes :)
Love it!
Thanks :-)
What`s wrong with prefers-color-scheme: dark?
Nothing wrong with that. using preferred color scheme is great, but also great to have a toggle.
Valuable information 👍🏻👍🏻
Feel free to ask me any questions about this if you're having trouble.
I'm surprised that there is no "Dark Mode with service worker" yet
Thank you for this idea, but i have no clue why dark mode does not work in my web platform. (I am using admin LTE template) tho there is no error in my browser's console.