Skip to content

[css-values-5] Using if() to do dark/light switching of image urls. #10577

@MattWilcox

Description

@MattWilcox

Please forgive me if this one's a bit vague as I'm adding an issue here at Chris Lilley's request, and I'm not familiar with the process too much.

There are times when I need to swap the background-image as well as text colour, and it's 100% dependant on the same logic as light-dark() value. But, you can't do this:

.thing {
  background-image: light-dark(
    url('/assets/light.svg'),
    url('/assets/dark.svg')
  );
}

I'd like to be able to do that instead of having to resort to media queries to do what in the end is the same thing, for the same reason, at the same time - but not working on a colour property. In my mind, light-dark() is a toggle to map key-value pairs based on a scheme - but does not need to be (and should not be) tied to outputting "colour" as a return value (or however this is currently implemented/specced).

In this specific case, I have a graphical decoration added to some link text, and need to change the decoration's colour to match the text colour. Because SVG images loaded in via CSS don't work the same as if they're embeded in HTML they will not inherit to text colour, and I have to specify an entirely different asset to load to match the text colour.

https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/light-dark

https://mstdn.social/@mattwilcox/112792513614070547

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Regular agenda

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions