aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/@media/prefers-color-scheme/index.html
blob: cb9c5c422f85b9a18fbdfb4bfb2c8d5001c5e4b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
---
title: prefers-color-scheme
slug: Web/CSS/@media/prefers-color-scheme
tags:
  - CSS
  - Web
  - prefers-color-scheme
  - 参考
  - 媒体查询
translation_of: Web/CSS/@media/prefers-color-scheme
---
<p><strong><code>prefers-color-scheme</code></strong> <a href="/zh-CN/docs/CSS">CSS</a> <a href="/zh-CN/docs/Web/CSS/Media_Queries/Using_media_queries#Media_features">媒体特性</a>用于检测用户是否有将系统的主题色设置为亮色或者暗色。</p>

<h2 id="语法">语法</h2>

<dl>
 <dt><code><dfn>no-preference</dfn></code></dt>
 <dd>表示系统未得知用户在这方面的选项。在<a href="https://drafts.csswg.org/mediaqueries-5/#boolean-context" id="ref-for-boolean-context④">布尔值上下文</a>中,其执行结果为 <code>false</code></dd>
 <dt><code><dfn>light</dfn></code></dt>
 <dd>表示用户已告知系统他们选择使用浅色主题的界面。</dd>
 <dt><code><dfn>dark</dfn></code></dt>
 <dd>表示用户已告知系统他们选择使用暗色主题的界面。</dd>
</dl>

<div class="standardNoteBlock">
<p><strong>译者注:</strong>“未得知”、“已告知”等用语,英文原文如此。<br>
 “未得知”可理解为:浏览器的宿主系统不支持设置主题色,或者支持主题色并默认为/被设为了未设置/无偏好。<br>
 “已告知”为:浏览器的宿主系统支持设置主题色,且被设置为了亮色或者暗色。</p>

<p>目前,若结果为 <code>no-preference</code>,无法通过此媒体特性获知宿主系统是否支持设置主题色,或者用户是否主动将其设置为无偏好。出于隐私保护等方面的考虑,用户或用户代理也可能在一些情况下在浏览器内部将其设置为 <code>no-preference</code></p>
</div>

<h2 id="样例">样例</h2>

<p>这个例子使用了一个有黑色背景和白色文字的元素,当用户使用浅色主题时,会翻转黑白颜色。</p>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;div class="day"&gt;Day (initial)&lt;/div&gt;
&lt;div class="day light-scheme"&gt;Day (changes in light scheme)&lt;/div&gt;
&lt;div class="day dark-scheme"&gt;Day (changes in dark scheme)&lt;/div&gt; &lt;br&gt;

&lt;div class="night"&gt;Night (initial)&lt;/div&gt;
&lt;div class="night light-scheme"&gt;Night (changes in light scheme)&lt;/div&gt;
&lt;div class="night dark-scheme"&gt;Night (changes in dark scheme)&lt;/div&gt;
</pre>

<h3 id="CSS">CSS</h3>

<pre class="brush: css">.day   { background: #eee; color: black; }
.night { background: #333; color: white; }

@media (prefers-color-scheme: dark) {
  .day.dark-scheme   { background:  #333; color: white; }
  .night.dark-scheme { background: black; color:  #ddd; }
}

@media (prefers-color-scheme: light) {
  .day.light-scheme   { background: white; color:  #555; }
  .night.light-scheme { background:  #eee; color: black; }
}

.day, .night {
  display: inline-block;
  padding: 1em;
  width: 7em;
  height: 2em;
  vertical-align: middle;
}
</pre>

<h3 id="结果">结果</h3>

<p>{{EmbedLiveSample("Examples")}}</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">注释</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>
    <p>{{SpecName('CSS5 Media Queries', '#descdef-media-prefers-color-scheme', 'prefers-color-scheme')}}</p>
   </td>
   <td>{{Spec2('CSS5 Media Queries')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("css.at-rules.media.prefers-color-scheme")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li><a href="https://www.youtube.com/watch?v=jmepqJ5UbuM">Video tutorial: Coding a Dark Mode for your Website</a></li>
 <li><a href="https://stuffandnonsense.co.uk/blog/redesigning-your-product-and-website-for-dark-mode">Redesigning your product and website for dark mode</a></li>
 <li>Changing color schemes in <a href="https://blogs.windows.com/windowsexperience/2019/04/01/windows-10-tip-dark-theme-in-file-explorer/">Windows</a>, <a href="https://developer.apple.com/design/human-interface-guidelines/macos/visual-design/dark-mode/">macOS</a> and <a href="https://www.theverge.com/2019/5/7/18530599/google-android-q-features-hands-on-dark-mode-gestures-accessibility-io-2019">Android</a>.</li>
</ul>

<div>{{QuickLinksWithSubpages("/zh-CN/docs/Web/CSS/@media/")}}</div>