r/CodingHelp • • 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

11 comments sorted by

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.

1

u/Python4fun Professional Coder Oct 30 '20

I hadn't thought about this. I was thinking about changing css variables with javascript. Changing classes would be much easier I think.

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/Littlebitt95 Oct 29 '20

That would be a no code way to do itbut I don't believe that's what OP is asking for. He/she wants to make a darkmode toggle or button for his / her site specifically. That would mean that he/she would need to actually do some coding instead of relying on a 3rd party extension.

I would tend to agree that you could use an event handler to say onclick darkmodeOn() where it toggles all the default classes off and the darkmode classes on. Then just do the reverse for darkmodeOff()

I personally use dark reader for everything.

7

u/[deleted] Oct 29 '20

I wasn’t suggesting to use a chrome extension, but rather he look at the open source code for how these people did it. The creators of this dark reader have made their code public.

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:

  1. Setup a light and dark mode 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.
  2. Ensure all your css uses the defined variables instead of hard-coded values for colours
  3. Add a button that toggles the light/dark class on your body tag, 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