Learn how to build The Ultimate Theme Toggle™️ for your website using JavaScript, CSS custom properties, local storage and system settings. No fram...
For further actions, you may consider blocking this person and/or reporting abuse
This is lovely, as a CSS wrangler I particularly love the text changing. Your implementation is simple and straightforward. Would love to implement it sometime maybe on my personal website which I am reworking into a nextJS site
Thank you! Good luck, please share the results with me when you're done! ✨
I managed to implement a dark/light theme toggle on this small WordPress website.
Cyberthrust Website
Nice! Make sure you follow some of the accessibility guidelines in the post to make sure screen readers understand what the toggle is for. Otherwise, works nicely!
Thank you for that observation totally overlooked it.
Great One @whitep4nth3r
Definitely saving this to use later! So cool!
Yes, pretty cool :D!
It's very nice of Salma Alam-Naylor to provide the code in Codepen to see how things work.
Currently, I'm working on a website which it has this feature, and it's wild that this is almost how I am working on this feature, if I could give an extra suggestion...
Usually we would use darker colors in light backgrounds and lighter colors in darker backgrounds to provide a good contrast. For doing this, I would recommend doing something like this:
Now, in our components, we would use the
--main-color
variable instead of--golden
and--yellowish-brown
to color our elements. So instead of adding specific colors to the elements depending on which theme is active, they would automatically change.Great post! I really enjoyed reading your insights and the way you presented your ideas was clear and engaging. Thank you for sharing your knowledge and perspective with us.
Thank you so much Eden! Glad you enjoyed it ☺️
We appreciate you developers. Makes our life enjoyable. Imagine choosing from light and dark theme wow
awesome 🙂
great job, congratulations !
Thank you.
Amazing article.
Helped me a lot
You're welcome!
I'd argue for a combination of both - use
prefers-color-scheme
to set the default, and then keep track of user settingBut if
prefers-color-scheme
isn’t available for some reason, you’ll need a default anyway.Yeah, fallbacks on fallbacks, simple
"While this works, it’s not classed as valid HTML and I can’t find any documentation on it! Any insight on this is much appreciated. 😅"
HTML represents DOM in text form. Any attribute name is valid, as long, as it is valid HTML/XML attribute name. data- convention means literally nothing than convention and treated exactly in same way as any other name. More over: all attributes handled in same way. So you must not say about validity just based on this. At least you should point in which schema or tool is considered invalid. However this is pointless, because, everything what works in browser - is perfectly valid.
The W3C markup validation service claims that
color-mode
on HTML is invalid.Check out this ready to use toggle which supports system theme and changes with animation:
github.com/mahozad/theme-switch