CSS has just added an image form of light-dark(). So as well as choosing between a pair of colors based on light or dark mode, you can also choose between a pair of images.
No you can't mix them because parsing would be hard.
#CSS
CSS has just added an image form of light-dark(). So as well as choosing between a pair of colors based on light or dark mode, you can also choose between a pair of images.
No you can't mix them because parsing would be hard.
#CSS
@svgeesus Great to see more progress on this feature.
Does this only include `url()` images or could we use gradients? I don't have a use-case in mind, but I could imagine a two-stop gradient in light mode, and three-stop gradient in dark mode, for example, where inlining `light-dark()` for each colour stop wouldn't work.
@chriskirknielsen @svgeesus ooooh this is exciting!
As far as I know it, the things that can react to color-scheme currently are:
- light-dark() (soon with images, ace!)
- default html "theme"
- system colors, especially CanvasText/Canvas (in chrome the others are a bit broken)
...is that it? I've heard of the coming of color-scheme() to be used in container queries and if().
@sarajw @chriskirknielsen It was explained by @bramus how to make a custom light-dark function:
Media queries are nice, but for many things, we don’t even need them: there is this great CSS property `color-scheme`, which allows us to make various things adapt to the current color scheme. We can even set it on a per-element basis. This post describes how we can use registered custom properties and style queries to read the current value of a `light-dark()` color for styling any non-color properties as well.
@kizu ooh yes thank you!
Full browser support for style queries is still lagging behind, I guess? That captured colour scheme is nice, though.
@sarajw @kizu I'm assuming I could have a UI control that sets user theme preference outside of the system preference (.dark on root etc.)
And have my CSS contain:
```
:root { color-theme: light dark }
.light { color-theme: light }
.dark { color-theme: dark }
```
Which I believe would work fine today with light-dark() use? But the future will include being able to switch background images and other properties too.
I still need to figure the nicest way to swap regular <img> based on theme.
@scrwd yes looks fine to me!
The issue with the image swap is when you're in default "light dark" mode where either could be the current one - things are moving to allow us to query which one is active one way or another.