Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

19449 prefers color scheme #20886

Merged
merged 22 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
be04bdb
docs(CSS): prefers-color-scheme for embedded content
bsmth Sep 14, 2022
d850ccd
docs(CSS): minor fix for coloer-scheme
bsmth Sep 15, 2022
61c1393
Merge branch 'mdn:main' into 19449-prefers-color-scheme
bsmth Sep 19, 2022
7847185
chore(build) replace Prettier config
bsmth Sep 19, 2022
8121b78
docs(CSS): add example of embedded SVG for prefers-color-scheme
bsmth Sep 19, 2022
eef8163
docs(CSS): add xrefs for color-scheme & prefers-color-scheme
bsmth Sep 19, 2022
5373e03
Merge remote-tracking branch 'upstream/main' into 19449-prefers-color…
bsmth Sep 20, 2022
f14d789
docs(CSS): improve wording for prefers-color-scheme docs
bsmth Sep 20, 2022
1568340
typo fix
bsmth Sep 20, 2022
1b90246
Merge branch 'main' into 19449-prefers-color-scheme
bsmth Oct 5, 2022
09fad27
Merge branch 'main' into 19449-prefers-color-scheme
bsmth Oct 6, 2022
ceb767c
docs(CSS) add details on secure animated mode for SVGs
bsmth Oct 6, 2022
3959d1e
Update files/en-us/web/css/@media/prefers-color-scheme/index.md
bsmth Oct 7, 2022
42b6f84
Update files/en-us/web/css/@media/prefers-color-scheme/index.md
bsmth Oct 7, 2022
43872c3
chore(docs) Clarify embedded SVG details, links to supporting docs
bsmth Oct 10, 2022
98f5ad6
Merge branch 'main' into 19449-prefers-color-scheme
bsmth Oct 10, 2022
8327843
Merge branch 'main' into 19449-prefers-color-scheme
bsmth Oct 12, 2022
95456f7
Merge branch 'main' into 19449-prefers-color-scheme
bsmth Oct 12, 2022
c8d5b52
Apply suggestions from code review
bsmth Oct 13, 2022
d8ca6e6
docs(CSS): Link to more appropriate inline SVG example, minor copy fix
bsmth Oct 13, 2022
7f26d39
Merge branch 'main' into 19449-prefers-color-scheme
bsmth Oct 13, 2022
2b315e1
grammar
estelle Oct 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ build/

# Ignore markdown files till full pass is made on each folder
*.md

bsmth marked this conversation as resolved.
Show resolved Hide resolved
66 changes: 59 additions & 7 deletions files/en-us/web/css/@media/prefers-color-scheme/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ browser-compat: css.at-rules.media.prefers-color-scheme

{{QuickLinksWithSubpages("/en-US/docs/Web/CSS/@media/")}}

The **`prefers-color-scheme`** [CSS](/en-US/docs/Web/CSS) [media feature](/en-US/docs/Web/CSS/Media_Queries/Using_media_queries#media_features) is used to detect if the user has requested a light or dark color theme.
The **`prefers-color-scheme`** [CSS](/en-US/docs/Web/CSS) [media feature](/en-US/docs/Web/CSS/Media_Queries/Using_media_queries#targeting_media_features) is used to detect if a user has requested light or dark color themes.
A user indicates their preference through an operating system setting (e.g. light or dark mode) or a user agent setting.

The user might indicate this preference through an operating system setting (e.g. light or dark mode) or a user agent setting.
## Embedded elements

For SVG and iframes, `prefers-color-scheme` lets you to set a CSS style for the SVGs or iframe based on the [`color-scheme`](/en-US/docs/Web/CSS/color-scheme) of parent element in the web page.
bsmth marked this conversation as resolved.
Show resolved Hide resolved
SVGs must be used embedded (i.e., `<img src="circle.svg" />`) as opposed to [inlined in HTML](/en-US/docs/Web/SVG/Tutorial/Getting_Started#a_simple_example).
bsmth marked this conversation as resolved.
Show resolved Hide resolved
An example of using `prefers-color-scheme` in SVGs can be found in in the [Color scheme inheritance](#color_scheme_inheritance) section.

Using `prefers-color-scheme` is allowed in SVG and iframe elements used [cross-origin](/en-US/docs/Web/Security/Same-origin_policy#cross-origin_network_access) from a different host than the page that's referencing them.
bsmth marked this conversation as resolved.
Show resolved Hide resolved
To learn more about SVGs, see the [SVG documentation](/en-US/docs/Web/SVG) and for more information about iframes, see the [iframe documentation](/en-US/docs/Web/HTML/Element/iframe).

## Syntax

Expand All @@ -27,9 +35,10 @@ The user might indicate this preference through an operating system setting (e.g

## Examples

The elements below have an initial color theme. They can be further themed according to the user's color scheme preference.
### Detecting a dark theme

### HTML
The elements below have an initial color theme.
They can be further themed according to the user's color scheme preference.

```html
<div class="day">Day (initial)</div>
Expand All @@ -42,7 +51,7 @@ The elements below have an initial color theme. They can be further themed accor
<div class="night dark-scheme">Night (changes in dark scheme)</div>
```

### CSS
The following CSS is used to style the elements above:

```css
.day {
Expand Down Expand Up @@ -86,9 +95,52 @@ The elements below have an initial color theme. They can be further themed accor
}
```

### Result
{{EmbedLiveSample("Detecting_a_dark_theme")}}

### Color scheme inheritance

The following example shows how to use `prefers-color-scheme` with the [`color-scheme` property](/en-US/docs/Web/CSS/color-scheme) inherited from a parent element.
A script is used to set the source of the `<img>` elements, this would normally be done in HTML as `<img src="circle.svg" />`.
bsmth marked this conversation as resolved.
Show resolved Hide resolved

On browsers that support this feature (such as Firefox), you should see three circles, with one drawn in a different color.
bsmth marked this conversation as resolved.
Show resolved Hide resolved
The first circle inherits the `color-scheme` from the OS and can be toggled using the system OS's theme switcher.

The second and third circles inherit the `color-scheme` from the embedding element and so the `@media` query allows to set styles of the SVG content based on the parent element's `color-scheme`.
estelle marked this conversation as resolved.
Show resolved Hide resolved
In this case, the parent element with a `color-scheme` CSS property is a `<div>`.

```html
<div>
<img />
</div>

<div style="color-scheme: light">
<img />
</div>
<div style="color-scheme: dark">
<img />
</div>

<!-- Embed an SVG for all <img> elements -->
<script>
for (let img of document.querySelectorAll("img")) {
img.src =
bsmth marked this conversation as resolved.
Show resolved Hide resolved
"data:image/svg+xml;base64," +
btoa(`
<svg width="32" height="32" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
<style>
:root { color: blue }
@media (prefers-color-scheme: dark) {
:root { color: purple }
}
</style>
<circle fill="currentColor" cx="16" cy="16" r="16"/>
</svg>
`);
}
</script>
```

bsmth marked this conversation as resolved.
Show resolved Hide resolved
{{EmbedLiveSample("Examples")}}
{{EmbedLiveSample("Color_scheme_inheritance")}}

## Specifications

Expand Down
1 change: 1 addition & 0 deletions files/en-us/web/css/color-scheme/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ To opt the entire page into the user's color scheme preferences declare `color-s

## See also

- [`prefers-color-scheme`](/en-US/docs/Web/CSS/@media/prefers-color-scheme) media query to detect user preferences for color schemes.
- [Applying color to HTML elements using CSS](/en-US/docs/Web/CSS/CSS_Colors/Applying_color)
- Other color-related properties: {{cssxref("color")}}, {{cssxref("background-color")}}, {{cssxref("border-color")}}, {{cssxref("outline-color")}}, {{cssxref("text-decoration-color")}}, {{cssxref("text-emphasis-color")}}, {{cssxref("text-shadow")}}, {{cssxref("caret-color")}}, and {{cssxref("column-rule-color")}}
- {{cssxref("background-image")}}
Expand Down