This page is a demo of using new CSS features to add a toggle that enables a dark mode
It is meant to be used on a per-site basis, not a per-page basis - please don't use this for a single page, as that would contribute to making accessibility across the entire site less consistent.
Here is the toggle:
Observe that:
- Enabling the toggle makes the whole page change appearance, even outside of the iframe
- Dark mode persists if the page is reloaded or navigated away from
- If this page is open in multiple tabs, toggling dark mode in one affects all of them
Applications:
This could be used to add a darkmode toggle to an entire site that remembers the user's choice. E.g. consider the following CSS, implemented as part of a site's base theme (e.g. Sigma-10):
/* By default, light theme (made up CSS variables for example purposes */
:root {
--text-color: black;
--background-color: white;
... etc
}
/* Enable dark mode if the user has configured their browser/device as such */
@media (prefers-color-scheme: dark) {
:root {
--text-color: white;
--background-color: black;
... etc
}
}
/* Enable dark mode if the above component is toggled on */
:root:has(.darkmode-detector iframe[style="height: 15px;"]) {
--text-color: white;
--background-color: black;
... etc
}
In lieu of that, here's the demo CSS that the toggle on this page applies:
.darkmode-detector iframe { height: 1rem !important; } /* Simple filter-based dark mode CSS for demonstration purposes */ :root:has(.darkmode-detector iframe[style="height: 15px;"]) { background: black; filter: invert(1) hue-rotate(180deg); } :root:has(.darkmode-detector iframe[style="height: 15px;"]) img { filter: invert(1) hue-rotate(180deg); }
Source code
<html lang="en"> <head> <script src="https://interwiki.scpwiki.com/resizeIframe.js" defer></script> <script> window.resize = () => {} function getDarkMode() { return Boolean(localStorage.getItem("dark-mode")) } function setDarkMode(choice) { localStorage.setItem("dark-mode", choice ? "true" : "") window.resize(choice ? 15 : 16) } addEventListener("load", () => { window.resize = window.resizeIframe.createResizeIframe( document.referrer, location.href.replace(/^.*\//, "/"), 100 ) const checkbox = document.querySelector("input") if (getDarkMode()) checkbox.checked = true setDarkMode(checkbox.checked) checkbox.addEventListener("change", () => { setDarkMode(checkbox.checked) }) // React to dark mode selections from other tabs/windows addEventListener("storage", event => { if (event.key === null) { // Caused by clearing all storage setDarkMode(false) checkbox.checked = false } if (event.key === "dark-mode") { setDarkMode(event.newValue) checkbox.checked = Boolean(event.newValue) } }) }) </script> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: sans-serif; font-size: 0.625rem; } label { display: flex; align-items: center; gap: 1ch; } </style> </head> <body style="height: 1rem"> <label> <input type="checkbox"> Dark mode </label> </body> </html>








