aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/var/index.html
blob: c79b5482d9c4b08ef5e9e5beba3c20e7794eb269 (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
---
title: var()
slug: Web/CSS/var
tags:
  - CSS
  - CSS 变量
  - CSS 方法
  - var()
  - 参考
  - 实验性的
translation_of: Web/CSS/var()
original_slug: Web/CSS/var()
---
<div>{{CSSRef}}</div>

<h2 id="概述">概述</h2>

<p><strong><code>var()</code></strong>函数可以代替元素中任何属性中的值的任何部分。<strong><code>var()</code></strong>函数不能作为属性名、选择器或者其他除了属性值之外的值。(这样做通常会产生无效的语法或者一个没有关联到变量的值。)</p>

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

<p>方法的第一个参数是要替换的自定义属性的名称。函数的可选第二个参数用作回退值。如果第一个参数引用的自定义属性无效,则该函数将使用第二个值。</p>

{{csssyntax}}

<div class="note">
<p>注意:自定义属性的回退值允许使用逗号。例如, <code>var(--foo, red, blue)</code> 将<code>red, blue</code>同时指定为回退值;即是说任何在第一个逗号之后到函数结尾前的值都会被考虑为回退值。</p>
</div>

<h3 id="值"></h3>

<dl>
 <dt>&lt;custom-property-name&gt; 自定义属性名</dt>
 <dd>在实际应用中它被定义为以两个破折号开始的任何有效标识符。 自定义属性仅供作者和用户使用; CSS 将永远不会给他们超出这里表达的意义。</dd>
 <dt>&lt;declaration-value&gt; 声明值(后备值)</dt>
 <dd>回退值被用来在自定义属性值无效的情况下保证函数有值。回退值可以包含任何字符,但是部分有特殊含义的字符除外,例如换行符、不匹配的右括号(如<code>)<font face="Open Sans, arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;"></span></font></code><code>]<font face="Open Sans, arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;"></span></font></code><code>}</code>)、感叹号以及顶层分号(不被任何非<code>var<strong>()</strong></code>的括号包裹的分号,例如<code>var(--bg-color, --bs<strong>;</strong>color)</code>是不合法的,而<code>var(--bg-color, --value<strong>(</strong>bs;color<strong>)</strong>)</code>是合法的)。</dd>
</dl>

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

<h3 id="Using_a_custom_property_set_on_root">在 :root 上定义,然后使用它</h3>

<pre class="brush: css notranslate">:root {
  --main-bg-color: pink;
}

body {
  background-color: var(--main-bg-color);
}
</pre>

<h3 id="Custom_properties_with_fallbacks_for_use_when_the_property_has_not_been_set">当第一个值未定义,回退值生效</h3>

<pre class="brush: css notranslate">/* 后备值 */

/* 在父元素样式中定义一个值 */
.component {
  --text-color: #080; /* header-color 并没有被设定 */
}

/* 在 component 的样式中使用它: */
.component .text {
  color: var(--text-color, black); /* 此处 color 正常取值 --text-color */
}
.component .header {
  color: var(--header-color, blue); /* 此处 color 被回退到 blue */
}

</pre>

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS3 Variables', '#using-variables', 'var()')}}</td>
   <td>{{Spec2('CSS3 Variables')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

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

<p>{{Compat("css.properties.custom-property.var")}}</p>

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

<ul>
 <li>{{cssxref("env","env(…)")}} – read‑only environment variables controlled by the user‑agent.</li>
 <li><a href="/en-US/docs/Web/CSS/Using_CSS_variables">Using CSS variables</a></li>
</ul>