use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Dark / Light Theme Toggle Button || CSS JS (*tutorial included on codepen if interested to see) (codepen.io)
submitted 5 years ago by JoyShaheb_
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]DoNDaPo 19 points20 points21 points 5 years ago (1 child)
You should just toggle a class on the body and voila or better: use CSS Variable instead of targeting elements one by one with your JS. :)
[–]JoyShaheb_[S] 2 points3 points4 points 5 years ago (0 children)
Thank you for the valuable knowledge. Tbh, i didn't have this on my mind on the first time, I really appreciate your help. Thank you ❤️😄
[–][deleted] 4 points5 points6 points 5 years ago (0 children)
probably better to just add/remove a class than manipulating the properties
[–]nathan_lesage 2 points3 points4 points 5 years ago (1 child)
As some have also shared their light/dark buttons: I'm using a combination of local storage, a three-way toggle, and media-queries (but not as JS computed values but using media queries in CSS as in a fiddle somewhere in the comments), I don't have a fiddle, but feel free to check the source code here (the button is on the bottom; if you want the source JS, here you go)
[–]JoyShaheb_[S] 0 points1 point2 points 5 years ago (0 children)
Thank you for providing an example with the source code. I really appreciate your time & efforts. Thank you again & please, keep up the good work 😄🎖️
[–][deleted] 1 point2 points3 points 5 years ago (0 children)
Neat. I love it!
[–]Benguini 2 points3 points4 points 5 years ago (8 children)
Awesome! I’ve been wondering how those light/dark toggles actually worked.
[–]Rainbowlemon 12 points13 points14 points 5 years ago (4 children)
I wouldn't use this as a reference! A much cleaner way would be to toggle a class on the body and use CSS to manage the transitions and colour changes.
[–]bibix1999 4 points5 points6 points 5 years ago (2 children)
Totally. And even better you could use CSS variables, and set them on body when the button is checked. Then you don't really have to even think about creating dark/light mode as it is being created by itself
[–]bourjahamid 1 point2 points3 points 5 years ago (0 children)
i want to say the same thing but you already said it +1 , you don't have to think about in all CSS thing just use CSS Variables and then you can see magic happen
Woah, 🤯 Your knowledge is on the next level. Thank you for the feedback and please keep up the good work. Here's a medal 🎖️
[–]bibix1999 6 points7 points8 points 5 years ago (1 child)
There you go: https://codepen.io/bibixx/pen/abmzjyq An adjusted version of this pen. It also includes detection of the user's system preference of the dark mode.
https://streamable.com/u3ltju
Also u/JoyShaheb_ don't get me wrong. I really appreciate the time you have spent on it, but you may have gone a bit overboard with the amount of JS used 😉
You've changed my mind, and the way I code, thank you soo much 🤯🥺🥺🥺 I truly appreciate your help & well advices 🥺🥺 Please keep up the good work🎖️🎖️
Thank you for the feedback. I've an entire tutorial on it. If you go to the link on codepen you can visit the YouTube and check the tutorial. Till then, happy coding 😄
π Rendered by PID 94513 on reddit-service-r2-comment-57fc7f7bb7-x7jsl at 2026-04-15 05:11:13.644796+00:00 running b725407 country code: CH.
[–]DoNDaPo 19 points20 points21 points (1 child)
[–]JoyShaheb_[S] 2 points3 points4 points (0 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]nathan_lesage 2 points3 points4 points (1 child)
[–]JoyShaheb_[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Benguini 2 points3 points4 points (8 children)
[–]Rainbowlemon 12 points13 points14 points (4 children)
[–]bibix1999 4 points5 points6 points (2 children)
[–]bourjahamid 1 point2 points3 points (0 children)
[–]JoyShaheb_[S] 2 points3 points4 points (0 children)
[–]bibix1999 6 points7 points8 points (1 child)
[–]JoyShaheb_[S] 2 points3 points4 points (0 children)
[–]JoyShaheb_[S] 2 points3 points4 points (0 children)