aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/width/index.html
blob: 3f687f1dde49858cc5380e5cee976632efe3aa06 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
---
title: width
slug: Web/CSS/width
tags:
  - CSS
  - 参考
  - 大小
  - 宽度
translation_of: Web/CSS/width
---
<div>{{CSSRef}}</div>

<p><strong><code>width</code></strong> 属性用于设置元素的宽度。<code>width</code> 默认设置<a href="/en-US/docs/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model#content-area">内容区域</a>的宽度,但如果 {{cssxref("box-sizing")}} 属性被设置为 <code>border-box</code>,就转而设置<a href="/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model#border-area">边框区域</a>的宽度。</p>

<div>{{EmbedInteractiveExample("pages/css/width.html")}}</div>

<p class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>

<p>{{ cssxref("min-width") }}{{ cssxref("max-width") }} 属性的优先级高于 {{ cssxref("width") }}</p>

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

<pre class="brush:css no-line-numbers">/* &lt;length&gt; values */
width: 300px;
width: 25em;

/* &lt;percentage&gt; value */
width: 75%;

/* Keyword values */
width: max-content;
width: min-content;
width: fit-content(20em);
width: auto;

/* Global values */
width: inherit;
width: initial;
width: unset;
</pre>

<p><code>width</code> 属性也指定为:</p>

<ul>
 <li>下面关键字值之一:<code><a href="https://wiki.developer.mozilla.org/zh-CN/docs/Web/CSS/width$edit#min-content">min-content</a></code><code><a href="https://wiki.developer.mozilla.org/zh-CN/docs/Web/CSS/width$edit#max-content">max-content</a></code><code><a href="https://wiki.developer.mozilla.org/zh-CN/docs/Web/CSS/width$edit#fit-content">fit-content</a></code><code><a href="https://wiki.developer.mozilla.org/zh-CN/docs/Web/CSS/width$edit#auto">auto</a></code></li>
 <li>一个长度值 <code><a href="https://wiki.developer.mozilla.org/zh-CN/docs/Web/CSS/width$edit#%3Clength%3E">&lt;length&gt;</a></code> 或者百分比值 <code><a href="https://wiki.developer.mozilla.org/zh-CN/docs/Web/CSS/width$edit#%3Cpercentage%3E">&lt;percentage&gt;</a></code></li>
</ul>

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

<dl>
 <dt>{{cssxref("&lt;length&gt;")}}</dt>
 <dd>使用绝对值定义宽度。</dd>
 <dt>{{cssxref("&lt;percentage&gt;")}}</dt>
 <dd>使用外层元素的容纳区块宽度(the containing block's width)的百分比定义宽度。</dd>
 <dt><code>auto</code></dt>
 <dd>浏览器将会为指定的元素计算并选择一个宽度。</dd>
 <dt><code>max-content</code> {{ experimental_inline }}</dt>
 <dd>元素内容固有的(intrinsic)合适宽度。</dd>
 <dt><code>min-content</code> {{ experimental_inline }}</dt>
 <dd>元素内容固有的最小宽度。</dd>
 <dt><code>fit-content</code> {{ experimental_inline }}</dt>
 <dd>取以下两种值中的较大值:
 <ul>
  <li>固有的最小宽度</li>
  <li>固有首选宽度(max-content)和可用宽度(available)两者中的较小值</li>
 </ul>
 可表示为:<code>min(max-content, max(min-content, &lt;length-percentage&gt;))</code></dd>
</dl>

<h3 id="形式化语法">形式化语法</h3>

<pre class="syntaxbox">{{csssyntax}}</pre>

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

<h3 id="默认宽度">默认宽度</h3>

<pre class="brush:css">p.goldie {
  background: gold;
}</pre>

<pre class="brush:html">&lt;p class="goldie"&gt;The Mozilla community produces a lot of great software.&lt;/p&gt;</pre>

<p>{{EmbedLiveSample('Default_width', '500px', '64px')}}</p>

<h3 id="像素_px_和字高_em">像素 px 和字高 em</h3>

<pre class="brush: css">.px_length {
  width: 200px;
  background-color: red;
  color: white;
  border: 1px solid black;
}

.em_length {
  width: 20em;
  background-color: white;
  color: red;
  border: 1px solid black;
}
</pre>

<pre class="brush: html">&lt;div class="px_length"&gt;以 px 度量的宽度&lt;/div&gt;
&lt;div class="em_length"&gt;以 em 度量的宽度&lt;/div&gt;</pre>

<p>{{EmbedLiveSample('Pixels_and_ems', '500px', '64px')}}</p>

<h3 id="百分比">百分比</h3>

<pre class="brush: css">.percent {
  width: 20%;
  background-color: silver;
  border: 1px solid red;
}</pre>

<pre class="brush: html">&lt;div class="percent"&gt;按照百分比度量的宽度&lt;/div&gt;</pre>

<p>{{EmbedLiveSample('百分比', '500px', '64px')}}</p>

<h3 id="max-content"><code>max-content</code></h3>

<pre class="brush:css;">p.maxgreen {
  background: lightgreen;
  width: intrinsic;           /* Safari/WebKit 使用了非标准的名称 */
  width: -moz-max-content;    /* Firefox/Gecko */
  width: -webkit-max-content; /* Chrome */
}</pre>

<pre class="brush:html">&lt;p class="maxgreen"&gt;The Mozilla community produces a lot of great software.&lt;/p&gt;</pre>

<p>{{EmbedLiveSample('max-content', '500px', '64px')}}</p>

<h3 id="min-content"><code>min-content</code></h3>

<pre class="brush:css">p.minblue {
  background: lightblue;
  width: -moz-min-content;    /* Firefox */
  width: -webkit-min-content; /* Chrome */
}</pre>

<pre class="brush:html">&lt;p class="minblue"&gt;The Mozilla community produces a lot of great software.&lt;/p&gt;</pre>

<p>{{EmbedLiveSample('min-content', '500px', '155px')}}</p>

<h2 id="无障碍考虑">无障碍考虑</h2>

<p>当页面放大以增加文本大小时,请确保 <code>width</code> 设置的元素不会被截断并且不会遮挡其他内容。</p>

<ul>
 <li><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable#Guideline_1.4_Make_it_easier_for_users_to_see_and_hear_content_including_separating_foreground_from_background">MDN Understanding WCAG, Guideline 1.4 explanations</a></li>
 <li><a href="https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-scale.html" rel="noopener">Understanding Success Criterion 1.4.4  | Understanding WCAG 2.0</a></li>
</ul>

<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>{{ SpecName('CSS3 Box', '#the-width-and-height-properties', 'width') }}</td>
   <td>{{Spec2('CSS3 Box')}}</td>
   <td>增加 <code>max-content</code>, <code>min-content</code>, <code>available</code>, <code>fit-content</code>, <code>border-box</code>, <code>content-box</code> 关键字。</td>
  </tr>
  <tr>
   <td>{{ SpecName('CSS2.1', 'visudet.html#the-width-property', 'width') }}</td>
   <td>{{Spec2('CSS2.1')}}</td>
   <td>明确了此属性对哪些元素有效。</td>
  </tr>
  <tr>
   <td>{{ SpecName('CSS1', '#width', 'width') }}</td>
   <td>{{Spec2('CSS1')}}</td>
   <td>初始定义。</td>
  </tr>
 </tbody>
</table>

<p>{{cssinfo}}</p>

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

<p>{{Compat("css.properties.width")}}</p>

<div class="hidden">
<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
</div>

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

<ul>
 <li><a href="/zh-CN/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model">框盒模型</a></li>
 <li>{{cssxref("height")}}</li>
 <li>{{cssxref("box-sizing")}}</li>
 <li>{{cssxref("min-width")}}</li>
 <li>{{cssxref("max-width")}}</li>
</ul>