@johannesodland has raised that, if the author sets <meta name=color-scheme content=dark>, it sets the document's used color scheme to dark, but there is no way for it to affect which HTML source element is selected based on the used color scheme.
An author might assume in this example that the HTML parser will select dark-mode-image.png to download, but it won't necessarily:
<!-- User has their color-scheme preference set to light mode. -->
<meta name="color-scheme" content="dark">
<picture>
<source srcset="dark-mode-image.png" media="(prefers-color-scheme: dark)" />
<img alt="" src="light-mode-image.png">
</picture>
There are two questions:
- How could the UA choose a dark color scheme image to fetch based on the page's supported color schemes set by
<meta name=color-scheme content=dark>?
- How could the UA choose a dark color scheme image to fetch based on the used color scheme (which can be changed for all descendents in the DOM tree using the
color-scheme CSS property)?
Both are difficult to solve because the HTML parser makes an immediate request for an image as soon as it identifies an image URL. When it encounters source elements in the picture element, it can only select a source using the information it has at parse time. At parse time, we do not know the used color scheme. The only thing we might know is the page's supported color schemes set by the <meta name="color-scheme"> tag.
Therefore, I think we are more concerned with solving Question 1. Both of these questions should be resolved in the HTML spec, but I think it's worth considering the CSSWG's thoughts, first.
Proposed solutions
Option 1. Propagate the page's supported color-scheme
Option 1a. propagate to prefers-color-scheme media query
Once the page's supported color scheme is parsed, when the HTML parser encounters a prefers-color-scheme media query in a source, it evaluates it using the page's supported color scheme. So in this example, it loads the dark-mode-image.png.
<meta name="color-scheme" content="dark">
<picture>
<source srcset="dark-mode-image.png" media="(prefers-color-scheme: dark)" />
<img alt="" src="light-mode-image.png">
</picture>
This solution doesn't introduce any new HTML syntax. Perhaps #7213 sets a precedent for this kind of behaviour?
Option 1b. propagate to a new media query document-color-scheme
@johannesodland suggests a new media query called document-color-scheme (or page-color-scheme) which gets the used colour scheme for the page determined from the.
<meta name="color-scheme" content="dark">
<picture>
<source srcset="dark-mode-image.png" media="(document-color-scheme: dark)" />
<img alt="" src="light-mode-image.png">
</picture>
Is it worth introducing a new media query for this use case? Would it be useful in a stylesheet, or would authors rather use @container color-scheme()?
Option 2. Create a new source element attribute to declare the color scheme
Introduce a new source attribute called colorscheme="light | dark".
Option 2a. Gets the used color scheme when lazy loaded
It only works if image lazy loading is set.
It specifies that the resource should only be loaded if the colour scheme matches the used color scheme.
<meta name="color-scheme" content="light">
<style>
div {
color-scheme: dark;
}
</style>
<div>
<picture>
<source srcset="dark-mode-image.png" colorscheme="dark" />
<img loading="lazy" alt="" src="light-mode-image.png">
</picture>
</div>
Could it be confusing fro authors that it only works with lazy loading?
Option 2b. Like Option 2a, but gets the page color scheme when eagerly loaded
If lazy loading is set, it specifies that the resource should only be loaded if the colorscheme attribute matches the used color scheme.
If eager loading is set, it specifies that the resource should only be loaded if the colorscheme attribute matches an evaluation of the page's supported color scheme.
<meta name="color-scheme" content="dark">
<div>
<picture>
<source srcset="dark-mode-image.png" colorscheme="dark" />
<img alt="" src="light-mode-image.png">
</picture>
</div>
Could it be confusing for authors because it works very differently depending on whether using lazy loading?
Option 3. Create a new source element attribute for container queries
@romainmenke mentioned in #10249 (comment)
Relevant issue on conditionally loading resources based on container queries: whatwg/html#10182
whatwg/html#10182 proposes a container attribute for the source element. Theoretically, someone could use the color-scheme container query (from #10577 (comment)) to do the following:
<meta name="color-scheme" content="dark">
<picture>
<source srcset="dark-mode-image.png" container="color-scheme(dark)" />
<img loading="lazy" alt="" src="light-mode-image.png">
</picture>
Perhaps it would work similarly to Option 2a.
Note
This was initially discussed in #10249 (comment)
@johannesodland has raised that, if the author sets
<meta name=color-scheme content=dark>, it sets the document's used color scheme todark, but there is no way for it to affect which HTMLsourceelement is selected based on the used color scheme.An author might assume in this example that the HTML parser will select
dark-mode-image.pngto download, but it won't necessarily:There are two questions:
<meta name=color-scheme content=dark>?color-schemeCSS property)?Both are difficult to solve because the HTML parser makes an immediate request for an image as soon as it identifies an image URL. When it encounters
sourceelements in thepictureelement, it can only select a source using the information it has at parse time. At parse time, we do not know the used color scheme. The only thing we might know is the page's supported color schemes set by the<meta name="color-scheme">tag.Therefore, I think we are more concerned with solving Question 1. Both of these questions should be resolved in the HTML spec, but I think it's worth considering the CSSWG's thoughts, first.
Proposed solutions
Option 1. Propagate the page's supported color-scheme
Option 1a. propagate to
prefers-color-schememedia queryOnce the page's supported color scheme is parsed, when the HTML parser encounters a
prefers-color-schememedia query in a source, it evaluates it using the page's supported color scheme. So in this example, it loads the dark-mode-image.png.This solution doesn't introduce any new HTML syntax. Perhaps #7213 sets a precedent for this kind of behaviour?
Option 1b. propagate to a new media query
document-color-scheme@johannesodland suggests a new media query called
document-color-scheme(orpage-color-scheme) which gets the used colour scheme for the page determined from the.Is it worth introducing a new media query for this use case? Would it be useful in a stylesheet, or would authors rather use
@container color-scheme()?Option 2. Create a new
sourceelement attribute to declare the color schemeIntroduce a new
sourceattribute calledcolorscheme="light | dark".Option 2a. Gets the used color scheme when lazy loaded
It only works if image lazy loading is set.
It specifies that the resource should only be loaded if the colour scheme matches the used color scheme.
Could it be confusing fro authors that it only works with lazy loading?
Option 2b. Like Option 2a, but gets the page color scheme when eagerly loaded
If lazy loading is set, it specifies that the resource should only be loaded if the
colorschemeattribute matches the used color scheme.If eager loading is set, it specifies that the resource should only be loaded if the
colorschemeattribute matches an evaluation of the page's supported color scheme.Could it be confusing for authors because it works very differently depending on whether using lazy loading?
Option 3. Create a new
sourceelement attribute for container queries@romainmenke mentioned in #10249 (comment)
whatwg/html#10182 proposes a
containerattribute for thesourceelement. Theoretically, someone could use the color-scheme container query (from #10577 (comment)) to do the following:Perhaps it would work similarly to Option 2a.