Dark / light mode toggle
Get a theme toggle for your website, like the one on this site.
If you want something incredibly simple, you can check out this W3 Schools page.
However, there are a few problems with this method. It's not comprehensive, won't remember your selection if you navigate to another page or refresh, and also doesn't take the user's dark/light mode system preferences into account.
So we want to create a theme toggle that:
- Is site-wide
- Defaults to the user's specified preference
- Remember's the user's selection and persists throughout their session
So I created something that did that. For an example, you can click the button below to see a preview:
Toggle Button HTML (place wherever you want on your site):What's happening here?
You're adding a button to your website users can click to toggle the dark/light theme styles we'll be defining with CSS soon. You can use any image you like, or you can use the two images below with their current names, light.gif and dark.gif.
Save these images to your images folder:
If you choose to use your own images, remember to replace https://create.david.qa/images/light.gif with the URL location of your version.
Toggle Button CSS (add to your stylesheet file):What's happening here?
You're positioning the button to go in the top right corner of your page and giving it the class we'll use to swap its image when clicked.
Remember to replace https://create.david.qa/images/dark.gif with the URL location of your versions of the images!
Dark Theme Styles CSS (add to your stylesheet file):What's happening here?
Here you're defining your dark mode styles, assuming your current theme is light by default.
This may not line up with your theme 1:1, so a good rule of thumb is to identify elements, classes, and ids that may have a color set, and then create a .dark-mode variant of it and change the color or background-color value to something else.
Toggle function JavaScript (create or add to a .js file):What's happening here?
Lots! First, this checks the user's system preference for dark or light theme (if any), stores that preference, and then displays it first. This only gets overridden if the user clicks the toggle, which then persists throughout the session across whatever other pages have this JavaScript running. Each time a page is loaded, it will check the storage if there's an existing preference set, and then display that version.
Remember to replace https://create.david.qa/images/dark.gif and https://create.david.qa/images/light.gif with the URL location of your versions of the images!
This JS will even store a log of which theme you're in, so you can view it in your browser's developer tools (under the "Console" tab).
As a bonus, you can add <meta name="darkreader-lock"> to the head of your page to disable the Dark Reader extension.
If you end up using this theme toggle, credit is appreciated (but not required)!
Changelog
- 2023.07.30
- Greatly simplified, provided example site.
- 2023.07.28
- Initial submission. Want to do more testing.