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 12 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
60 changes: 54 additions & 6 deletions files/en-us/web/css/@media/prefers-color-scheme/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ 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 user might indicate this preference through an operating system setting (e.g. light or dark mode) or a user agent setting.

Using `prefers-color-scheme` in embedded elements allows you to style a component based on the `color-scheme` of the parent element.
This applies to both same and cross-origin SVG and iframe elements and prevents cases where OS and browser preferences for color schemes are conflicting.

SVG elements must be referenced in [secure animated mode](https://svgwg.org/specs/integration/#secure-animated-mode) to use `prefers-color-scheme`.
SVGs are in secure animated mode when referenced by `<img>` elements, used to define font glyphs, and loaded as resources for `mask`, `pattern`, gradient, or other resource elements.

bsmth marked this conversation as resolved.
Show resolved Hide resolved
## Syntax

- `light`
Expand All @@ -27,9 +32,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 +48,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 +92,50 @@ 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 inherited from a parent element.
A script is used to specify the source of the image 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 browser or OS and can be toggled using this page's theme switcher.
The second and third circle inherit the `color-scheme` from the embedding element and so the `@media` query allows to specify styles of the embedded content based on the parent element's `color-scheme`.
bsmth marked this conversation as resolved.
Show resolved Hide resolved

```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 All @@ -100,6 +147,7 @@ The elements below have an initial color theme. They can be further themed accor

## See also

- {{cssxref("color-scheme")}} CSS property
- [Simulate prefers-color-scheme in Firefox](https://firefox-source-docs.mozilla.org/devtools-user/page_inspector/how_to/examine_and_edit_css/index.html#view-media-rules-for-prefers-color-scheme) (Firefox Page Inspector > Examine and edit CSS)
- [Video tutorial: Coding a Dark Mode for your Website](https://www.youtube.com/watch?v=jmepqJ5UbuM)
- [Redesigning your product and website for dark mode](https://stuffandnonsense.co.uk/blog/redesigning-your-product-and-website-for-dark-mode)
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