Dark mode has been around for quite a long time now and you see a lot of websites embracing the power of what it can do by applying themes to a website. This feature was firstly available on Apple devices via iOS & MacOs systems and following them Windows and Google have done the same.

Image of macOS “General” system preferences
In this blog, I am going to talk about how to apply styles based on the user’s system preferences, how to toggle dark mode and light mode using localStorage and how to prevent “flash of un-styled theme” when the user has refreshed the page
Applying styles based on system preferences
In CSS we have a media query that can apply styles to our webpage based on what colour scheme the user has selected in the image above. The media query is called as prefers-color-scheme and this is how we use it in CSS:
:root {
--color-body-bg: #f0f0f0;
}
@media (prefers-color-scheme: dark) {
:root {
--color-body-bg: #2d2d2d;
}
}
@media (prefers-color-scheme: light) {
:root {
--color-body-bg: #ffffff;
}
}How to tell the browser which colour schemes we support
If you wish to limit the colour schemes to have on your website, a meta tag can be added at the head of the document which will tell the browser which colour schemes can be used on this website.
<!-- Dark is the preferred theme however the page supports both light and dark -->
<meta name="color-scheme" content="dark light">The browser will now style the default user agent elements such as button, input, select etc.. to match the colour scheme selected from the user’s device.
Switching between colour schemes
Now we have a way of applying a theme for each colour scheme we now can extend the behaviour by setting a toggle that will switch between the colour schemes regardless of what’s defined in system preferences.
Setting up the styles
The first thing we need to do before switching any colour scheme is to add some new classes to the styles we had above.
:root {
--color-body-bg: #f0f0f0;
}
@media (prefers-color-scheme: dark) {
:root,
.dark-mode {
--color-body-bg: #2d2d2d;
}
.light-mode {
--color-body-bg: #ffffff;
}
}
@media (prefers-color-scheme: light) {
:root,
.light-mode {
--color-body-bg: #ffffff;
}
.dark-mode {
--color-body-bg: #2d2d2d;
}
}What will happen now is that if the user’s preferences are dark mode and they select our button to switch to light, it will update the HTML element with a class of ‘light-mode’ that will override the dark mode styles. The next step is to remember what was selected in localStorage.
Remembering what was selected
There are two approaches to remembering what was selected by the user which are:
- Saving to local storage — mainly used for when you wish to retrieve the theme style from the client-side
- Saving to cookies — when you want to apply the class to the HTML element on the server-side
Since our example is only a static site we want to save our theme choice to local storage. To do this we need to use a tiny bit of JavaScript to save the selected theme.
Html
<!-- data attribute purely for finding the element -->
<button type="button" data-toggle-theme-button>Toggle theme</button>JavaScript
const LIGHT_MODE = 'light-mode'
const DARK_MODE = 'dark-mode'
const STORAGE_KEY = 'my-website-theme'
const BUTTON_SELECTOR = '[data-toggle-theme]'
const elHTML = document.querySelector('html')
const button = document.querySelector(BUTTON_SELECTOR);
const setTheme = (was, now) => {
elHTML.classList.add(now);
elHTML.classList.remove(was);
localStorage.setItem(STORAGE_KEY, JSON.stringify(now));
}
const handleButtonClick = () => {
// get the current theme from localStorage
const currentTheme = localStorage.getItem(STORAGE_KEY)
// If the current theme and its light mode then set it to dark mode else if its dark then set it to light
if (currentTheme) {
if(JSON.parse(currentTheme) === LIGHT_MODE) {
setTheme(LIGHT_MODE, DARK_MODE)
} else {
setTheme(DARK_MODE, LIGHT_MODE)
}
} else {
// if localStorage is not defined then we want to use whatever the media color-scheme is to switch it to the opposite
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
setTheme(DARK_MODE, LIGHT_MODE)
} else {
setTheme(LIGHT_MODE, DARK_MODE)
}
}
}
button.addEventListener('click', handleButtonClick)How to prevent “flash of un-styled theme”
Once we have set our own theme preference we now need to figure out how to retrieve the value of what the user selected before the styles start to paint the browser. We do this by adding some critical JavaScript before loading the stylesheet. This will apply to a class that will override the default colour scheme styles.
<script>
const LIGHT_MODE = 'light-mode'
const DARK_MODE = 'dark-mode'
const STORAGE_KEY = 'my-website-theme'
const elHTML = document.querySelector('html')
const storedTheme = localStorage.getItem(STORAGE_KEY)
if (storedTheme && JSON.parse(storedTheme) === LIGHT_MODE) {
elHTML.classList.add(LIGHT_MODE)
}
if (storedTheme && JSON.parse(storedTheme) === DARK_MODE) {
elHTML.classList.add(DARK_MODE)
}
</script>
<!-- load stylesheet here -->Other approaches
If your website can SSR (server-side render), another way would be to use a cookie which will then be accessible on the server-side. Once on the server side, you can add the class to the HTML element without any javascript blocking the render of the page.
Example
See the demo on CodePen: A demo of colour scheme toggle from CodePen
If you are like me, I have dark mode on for everything. When my friend showed me this feature on macOS a while ago this is how I describe the moment!👇🏽
Thanks for reading.
Resources
- browser support — https://caniuse.com/?search=prefer-color-scheme
- meta tag colour scheme — https://html.spec.whatwg.org/multipage/semantics.html#meta-color-scheme
- A complete guide to dark mode on the web — https://css-tricks.com/a-complete-guide-to-dark-mode-on-the-web/
Originally published on Medium.
