33 points · by dulvui · 3y ago · 53 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...
I used this in the 90's:
`<body bgcolor="#000000">`
You needed it to really appreciate the sparks from animated fire gifs.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/me...
Then in CSS setting both the prefers-color-scheme media queries but also configuring how the colors should react when the checkbox state is changed. So if a user has prefers-color-scheme:light then when the theme switcher is clicked it styles the page the opposite (example selector: .theme-switch:checked ~ .pagebody). Can also add such styling as a default outside of media queries, too, for browsers that don't recognize prefers-color-scheme.
Saving the state across pages however requires some JS but just wanted to comment that CSS-only toggles are possible.
The vast majority of users don’t know how to do that. I’m sure the vast majority of that group couldn’t really explain themes as a concept.
We could start a long discussion, should websites just follow system settings, or add their own custom controls, where you have different settings for each website. Along with dark mode switching, we should consider accessibility controls, and cookie settings too.
From a developer standpoint I'd prefer to reflect system settings.
Sites should follow the browser scheme (which usually follows OS) and can optionally provide a button to switch. This is possible with CSS only but too convoluted; one liner JS is enough for such optional cases.
What could you possibly be basing this theory on?
`color-scheme: light dark`
which automatically switches the color scheme of default html elements depending on the preferred color scheme. No theming required!
https://codepen.io/theprojectsomething/pen/oNQYqRw
Note: Could be simplified with the :has selector but it isn't currently supported in Firefox.
The biggest challenge is images. You have to mind images with transparent backgrounds, as well as SVG icons with a fixed colour.
Why not simply use native CSS variables? Worth pointing out since this is a post about doing something in a simple way.