42
loading...
This website collects cookies to deliver better user experience
<head></head>
of the document. This should be inlined into the top of the head so that it is executed as early as possible on page load.<script type="text/javascript">
document.documentElement.classList.toggle(
'dark',
localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)
);
</script>
dark
class to the HTML tag if localStorage.theme
is set to dark or prefers-color-scheme
is dark. dark
class on the <html>
tag. document.addEventListener('DOMContentLoaded', () =>
document.querySelectorAll('[toggle-dark-mode]').forEach((item) =>
item.addEventListener('click', () => {
localStorage.setItem('theme', localStorage.theme == 'dark' ? 'light' : 'dark');
document.documentElement.classList.toggle('dark');
})
)
);
toggle-dark-mode
HTML attribute. Clicking the element should then update local storage and toggle the dark mode class. <button toggle-dark-mode>Toggle dark mode</button>
dark:
prefix to any class names for modifications in dark mode. :root {
--color-bg: #fff;
}
html.dark {
--color-bg: #000;
}
body {
background-color: var(--color-bg);
}
:root {}
. html.dark {}
selector, which will override the root properties.