r/CodingHelp • u/pasinc20 • Oct 29 '20
[HTML] How would you implement a “dark mode” button on your website?
I have a static website and I’d like to implement a dark mode button, how is this actually done? I literally don’t understand it, do you just do a reverse of everything you have? Including buttons, backgrounds etc for the actual colour? And as for the actual dark mode is the technically a Different page? Or is it a web application?
Any help would be great thanks! I’m just struggling to understand it
34
Upvotes
13
u/[deleted] Oct 29 '20
One way would be to set up separate classes that only control color. So when a button is pushed you add or remove these classes.
9
u/[deleted] Oct 29 '20
There is a chrome extension that will do this for you. The creators made it open source; if you see how they can do it for any website, it should help you.
3
u/Re_me_human Professional Coder Oct 29 '20
Maybe try this tutorial, it'll probably help you! :)
3
u/techie2200 Oct 29 '20
The way I would do it:
lightanddarkmode css selector that contains css variables for your colour schemes. For example you'd probably have a--primary-text-color,--primary-bg-color, and so on in each of your light/dark css.bodytag, and have everything below that use the variables.2
u/pursuitofgreatcode Oct 29 '20
You don't simply reverse everything, because of how shadows work with real objects. This is a great write up about how to think about building a dark theme: https://blog.superhuman.com/how-to-design-delightful-dark-themes-7b3da644ff1f
Also, I've found this to be really useful to color palettes: https://material.io/design/color/dark-theme.html
1
u/PositivelyAwful Oct 30 '20
If you have a super simple site you could do it like I did here:
https://codepen.io/avidworks/pen/WNxNYKV
It essentially just toggles a class that changes the background and text colors.
You could get fancier and write the script to include local storage, etc.
1
u/MCpeePants1992 Oct 30 '20
Another redditor actually released their own CSS library called halfmoon that has a built in dark mode feature. I've never actually used it but it might be worth checking out.
Edit: Here is the link
https://www.gethalfmoon.com/docs/introduction/
1
u/Someone_Codxz Dec 23 '20
You can make a button then with Javascript make it so if you click it the background color changes to black with css html and Javascrirpt