Giving your users a way to customise the interface to their preference is a huge win for user experience. Here we are going to provide the user wit...
For further actions, you may consider blocking this person and/or reporting abuse
Just how we do it with dev.to!
CSS variables are awesome.
Wait, what! There is dark theme for dev.to, nobody told me about that. 😂
Dark mode is new, and it's fabulous (with a few kinks that still need to be worked out).
Oh! I see. How can I enable dark mode?
Settings ➡ Misc. On that page you'll find it under "Style Customization"
Awesome, thanks. Looks cool.
Love the dark mode on dev.to! 🙂
@Ananya Neogi
Hi,
Dark mode does not not work link open new window (target="_blank" links)
is there a solution?
Thanks
Best regards
Where is bro? Just tell me where is it? I didn't know about it...
Hey just found a simple error in your CSS
.theme-switch-wrapper {
display: flex;
align-items: center;
em {
margin-left: 10px;
font-size: 1rem;
}
}
You have a nested selector here, this does not work in CSS only SCSS
so to fix it I just broke out the one selector to be on its own and not nested one!
.theme-switch-wrapper {
display: flex;
align-items: center;
}
.theme-switch-wrapper em {
margin-left: 10px;
font-size: 1rem;
}
CSS variables are fantastic for use cases like this. Here's a <dark-mode-toggle> that initially respects
prefers-color-scheme
and allows for manual overrides. Read more about it in this article.Just added mine over at felixparadis.com :)
Quick note about accessibility though:
If you just
display:none;
your input, you're making it unaccessible through keyboard navigation.Just hide it behind the label instead and everyone can toggle 🎉
Then add
aria-label="Switch visual theme"
to your input and blind users can know what's it about.Yes, this makes more sense. Thanks! 🙂
I'll update the demo.
Thanks for this! I haven't tried my own light/dark mode since I tried implementing it on my first bootcamp project a couple years ago. I technically got it to work, but the code was not this DRY as I made two individual buttons that would add/remove a "dark" or "light" class on the entire body element. 😂 Your solution is much better!
Nice post 👌👌 🤩
Gonna embed into my portfolio
Thanks for this great post Ananya. I followed the instructions and managed to get the page up and running. But i am getting a console log Error: script5007 unable to get property 'addEventListener' for undefined or null source. Could someone please help me with a solution. Thanks.
Very cool.
Have you thought about the prefers-color-scheme media feature? How could this be implemented in this script? You should check that too In my opinion. The toggle would have to automatically switch to the preferred color scheme.
I'm planning to experiment with something like this, but using CSS only (no javascript).
I keep trying to serve it via ng serve but instead i get the error "Property 'checked' does not exist on type 'Element" referring to "if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
}"
You won't believe it, but actually you can nowadays even take the operation system setting for the dark mode and apply/get that, so your users don't even need that toggle, or at least use the version they prefer, by default.
With CSS and JS…
In my case, I would offer two toggle for users which are auto mode (system dark mode) and manual Light/Dark toggle, so users can choose whatever they prefer.
this doesn't work for me. console says 'toggleSwitch is null'
add the code after the switch in tags
Add the JS inside DOMContentLoaded method
this is great and so helpful - thanks so much! I do have one question - in the js, where does "light" come from? unless I'm missing it, that's not defined in the CSS, is it?
Hi Desi!
You're right, it's not defined anywhere.
If CSS doesn't find any specific styles related to light it will default back to the normal variables defined in
:root
.Why I added it?
I added it so that we can use it while checking for the saved theme in localStorage further and add it to the
root element
. It will also be useful in cases where you might want to add some specific styles for light apart from the default styles.I hope this helps 🙂
Awesome, thanks so much! Explanation makes perfect sense.
You're welcome 🙂
How we can set this for all pages?
Great post. I would love to add a Dark theme to my website, but probably won’t have the time 😭. Bookmarked your article anyways 🤓
Thank you for taking the time to write this article, it has been a great help! Would you follow this for all types of run time theming too, or do you think there is a better way to have multiple themes which people can switch between?
Super duper thanks for article.
Thanks! It will help me a lot. I've been using it wrong (but working), now you've cleared my mind. :)
Thank you Ananya! Great article, loved the tip about how to come up with a night theme :)
Amazing work
Thanks Ananya, it's helpful for me
Thank you for this article, I implemented it on my portfolio. Here victoreke.netlify.app/
Very Cool! Trying to figure out how to toggle between bg color & bg picture.
Hello nice work, really help me a lot!!!
Hey, Nice article. I have one question .. what if i have two bootstrap based css files (theme-light.css and theme-dark.css) ?
Nice, simple and works well even while using .less .
Thank you so much!
Great, thanks a lot!
I added an innerHTML to change the text as well:
A big thank! I was violently disabling dark-theme-CSS-sheet via JS for my blog, and it was creepy. This solution is way, way, way... way better!
I'm happy now.
How about images like the Logo in Header & Footer? How would you suggest making this happen?
add an image source changer in the functions or make the images a background image if you wanna change them with CSS
I was exactly looking for something like this. Thanks
Really enjoyed this tutorial. I just had to include it in a codepen of my own. You can view it here - codepen.io/HikwaMehluli/full/jOPPEVx
AWESOME!!! <3
Hi,
Dark mode does not not work link open new window (target="_blank" links)
is there a solution?
Thanks
Best regards
I visited your website and just loved it.
Could you help me with websites like this please.
Which language have you used for Backend? Could you make a tutorial for a simple blog website please.
Amazing 🙌 Thank you! 🙏
Such a simple solution! Who needs a plugin when you can just use CSS variables!
Nice
Is there any way to not have the background flash the "light" color before switching to "dark"? Little jarring to the user experience.
Nice post, Thanks for writing 😁
This isn't not working anymore?
Good job, exactly what I was looking for. I made an account here just to say this. Thank you!
does is remember the mode once I refresh the page
This is probably a dumb question, but I'm confused on where I add the javascript?
This was indeed a great article! Always wondered how to efficiently implement theming just using CSS Variables, and here it is! Good job!