by austin-cheney · 3y ago · 0 comments
1) Remove ALL color information from your CSS files and relocate that into a new CSS file only for color information. That is your default color scheme.
2) In that color scheme file prefix each rule with the same class name, possibly colorDefault.
3) Create a second color only CSS file with dark colors. This is your dark color scheme. Change the colorDefault class name to something else, possibly: colorDark.
4) Include those color files in your site using either media queries from the primary CSS or link tags in the HTML.
5) Finally add class name colorDefault to your HTML body tag. Use JavaScript to change that one class name as the user selects a different color scheme on user interaction.
Here is an example:
https://github.com/prettydiff/share-file-systems/tree/master...
- used css-vars for all colors throught code
- named those vars semantically after what their layout role is (e.g. --table-border-color, --link-color, ...)
- create 2 media-queries (or one default definition and one queried overwrite) with @media (prefers-color-scheme: light) at the root, in which I assigned those layout-role-named colors their dark-/light mode specific color (--link-color: #aa1100)
- turned those colors that I assign into css-vars themselves, named after ... well, their color (e.g. --link-color: var(--brandname-red-light), ...)
Works remarkebly well (almost didn't need another prefers-color-scheme* anywhere else). But in the end JS was needed to make it toggable and persistable within localstorage. But the only neccessary change in CSS was to turn the refers-color-scheme*-media queries into classnames.
The browser should allow per-site toggles for prefers-color-scheme. Just like how (some?) browsers ship with page style selectors using CSS alternate tags. e.g. on Firefox Alt > View > Page Styles.
https://developer.mozilla.org/en-US/docs/Web/CSS/Alternative...