46
loading...
This website collects cookies to deliver better user experience
color-scheme
property to your webpages.<!--
The page supports both dark and light color schemes,
and the page author prefers dark.
-->
<meta name="color-scheme" content="dark light">
/*
The page supports both dark and light color schemes,
and the page author prefers dark.
*/
:root {
color-scheme: dark light;
}
color-scheme
, you can even use it together with your dark mode toggle by using css, for example, one of the sites I made for a client josephojo.comcolor-scheme
property you can turn form elements, webpage background, text color, and scrollbars dark, a more famous example would be, github,color-scheme
css property together with @media (prefers-color-scheme: dark) {}
and the .dark
class, the final result ishtml {
color-scheme: light;
}
html.dark {
color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
html:not([data-theme]) {
color-scheme: dark;
}
}
module.exports = {
darkMode: 'class',
// ...
}
tailwindcss
before, it's basically the same as defining a class that when added to the html element will signal that the site is in dark mode.<html class="dark">
<!-- ... -->
</html>
You: Wait, but, how did you handle the theme toggle button?
Me: I'm glad you asked.
html:not([data-theme])
in the prefers-color-scheme: dark
media query,/* ... */
@media (prefers-color-scheme: dark) {
html:not([data-theme]) {
color-scheme: dark;
}
}
/* ... */
html.dark
represents the dark theme applied by tailwind
and [data-theme]
represents the currently applied theme, if data-theme
is different from the local storage, then the theme was manually toggled and the page should use the new theme in data-theme
as well as update the local storage theme, otherwise, it should use the local storage theme as data-theme
, but because data-theme
is only applied to the html
element after javascript is loaded we can tell our css to use the default dark theme if prefers-color-scheme: dark
and the html element doesn't have the data-theme
attribute.// Based on [joshwcomeau.com/gatsby/dark-mode/]
let getSavedTheme = () => {
const theme = window.localStorage.getItem("theme");
// If the user has explicitly chosen light or dark,
// let's use it. Otherwise, this value will be null.
if (typeof theme === "string") return theme;
// If they are using a browser/OS that doesn't support
// color themes, let's not do anything.
return null;
};
let saveTheme = (theme) => {
// If the user has explicitly chosen light or dark, store the default theme
if (typeof theme === "string")
window.localStorage.setItem("theme", theme);
};
let mediaTheme = () => {
// If they haven't been explicitly set, let's check the media query
const mql = matchMedia("(prefers-color-scheme: dark)");
const hasMediaQueryPreference = typeof mql.matches === "boolean";
if (hasMediaQueryPreference) return mql.matches ? "dark" : "light";
};
const html = document.querySelector("html");
// Get theme from html tag, if it has a theme or get it from localStorage
let checkCurrentTheme = () => {
let themeAttr = html.getAttribute("data-theme");
if (themeAttr) return themeAttr;
return getSavedTheme();
};
// Set theme in localStorage, as well as in the html tag
let applyTheme = (theme) => {
html.className = theme;
html.setAttribute("data-theme", theme);
};
try {
// if there is a saved theme in local storage use that,
// otherwise use `prefer-color-scheme` to set the theme
let theme = getSavedTheme();
if (theme == null) theme = mediaTheme();
// set the initial theme
html.setAttribute("data-theme", theme);
html.classList.add(theme);
// If a user changes the system/browser/OS theme, update the site theme as well,
// but don't save the change in local storage
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (e) => {
applyTheme(e.matches ? "dark" : "light");
});
// On theme toggle button click, toggle the page theme between dark and light mode,
// then save the theme in local storage
document
.querySelector("#theme-toggle")
.addEventListener("click", () => {
let theme = checkCurrentTheme() === "dark" ? "light" : "dark";
applyTheme(theme);
saveTheme(theme);
});
} catch (e) {
console.warn("Theming isn't available on this browser.", e);
}
color-scheme
.color-scheme
on web.devcolor-scheme
in the comments below.color-scheme
meta tag is that Samsung Internet won't force dark mode on your site if it uses the color-scheme
meta tag, from what I can tell Chrome might implement a similar feature in the future. I tweeted about it