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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
---
title: width
slug: Web/CSS/width
translation_of: Web/CSS/width
---
<div>{{CSSRef}}</div>
<p>The <strong><code>width</code></strong> CSS property specifies the width of an element. By default, the property defines the width of the <a href="/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model#content-area">content area</a>. If {{cssxref("box-sizing")}} is set to <code>border-box</code>, however, it instead determines the width of the <a href="/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model#border-area">border area</a>.</p>
<pre class="brush:css no-line-numbers">/* <length> values */
width: 300px;
width: 25em;
/* <percentage> value */
width: 75%;
/* Keyword values */
width: 25em border-box;
width: 75% content-box;
width: max-content;
width: min-content;
width: available;
width: fit-content;
width: auto;
/* Global values */
width: inherit;
width: initial;
width: unset;
</pre>
<div class="hidden" id="width">
<pre class="brush: html"><div class="grid">
<div class="col">
<div class="cell">
&lt;length&gt; values
<p class="w1">width: 150px</p>
<p class="w2">width: 20em</p>
<p class="w3 warning" title="this feature is experimental and might not work in your browser">width: 20em content-box</p>
<p class="w4 warning" title="this feature is experimental and might not work in your browser">width: 20em border-box</p>
</div>
<div class="cell">
&lt;percentage&gt; values
<p class="w5">width: 75%</p>
<p class="w6 warning" title="this feature is experimental and might not work in your browser">width: 75% content-box</p>
<p class="w7 warning" title="this feature is experimental and might not work in your browser">width: 75% border-box</p>
</div>
<div class="cell">
Keyword values
<p>width: auto</p>
<p class="w8 warning" title="this feature is experimental and might not work in your browser">width: max-content</p>
<p class="w9 warning" title="this feature is experimental and might not work in your browser">width: min-content</p>
<p class="w10 warning" title="this feature is experimental and might not work in your browser">width: available</p>
<p class="w11 warning" title="this feature is experimental and might not work in your browser">width: fit-content</p>
</div>
</div>
</div></pre>
<p>sd</p>
<pre class="brush: css">html,body {
height: 100%;
box-sizing: border-box;
}
.grid {
width: 100%;
height: 100%;
display: flex;
background: #EEE;
font: 1em monospace;
}
.col {
display: flex;
flex: 1 auto;
flex-direction: column;
}
.cell {
margin: .5em;
padding: .5em;
background-color: #FFF;
overflow: hidden;
text-align: left;
font-style: italic;
}
p {
font-size: 1rem;
font-style: normal;
background: #E4F0F5;
padding: .5em;
margin: .5em;
}
.warning {
background: #E4E4E4;
}
.warning:before {
content: "⚠️ ";
}
/* values */
.w1 { width: 150px; }
.w2 { width: 20em; }
.w3 { width: 20em content-box; }
.w4 { width: 20em border-box; }
/* value */
.w5 { width: 75%; }
.w6 { width: 75% content-box; }
.w7 { width: 75% border-box; }
/* Keyword values (mostly experimental) */
.w8 { width: max-content; }
.w9 { width: min-content; }
.w10 { width: available; }
.w11 { width: fit-content; }</pre>
</div>
<div>{{EmbedLiveSample("width", "100%", 660, "", "", "example-outcome-frame")}}</div>
<div class="note">
<p><strong>注意:</strong> {{cssxref("min-width")}} 和 {{cssxref("max-width")}} 會覆寫 {{cssxref("width")}}.</p>
</div>
<div>{{cssinfo}}</div>
<h2 id="Syntax">Syntax</h2>
<p>The <code>width</code> property is specified as either:</p>
<ul>
<li>one of the following keyword values: <code><a href="#available">available</a></code>, <code><a href="#min-content">min-content</a></code>, <code><a href="#max-content">max-content</a></code>, <code><a href="#fit-content">fit-content</a></code>, <code><a href="#auto">auto</a></code>.</li>
<li>a <code><a href="#<length>"><length></a></code> or a <code><a href="#<percentage>"><percentage></a></code>. This may optionally be followed by one of the following keywords: <code><a href="#border-box">border-box</a></code>, <code><a href="#content-box">content-box</a></code>.</li>
</ul>
<h3 id="Values">Values</h3>
<dl>
<dt><a id="<length>" name="<length>">{{cssxref("<length>")}}</a></dt>
<dd>Defines the width as an absolute value.</dd>
<dt><a id="<percentage>" name="<percentage>">{{cssxref("<percentage>")}}</a></dt>
<dd>Defines the width as a percentage of the containing block's width. If the width of the containing block depends on the width of the element, the resulting layout is undefined.</dd>
<dt><code><a id="border-box" name="border-box">border-box</a> </code>{{experimental_inline}}</dt>
<dd>If present, the preceding {{cssxref("<length>")}} or {{cssxref("<percentage>")}} is applied to the element's border box.</dd>
<dt><a id="content-box" name="content-box"><code>content-box</code></a> {{experimental_inline}}</dt>
<dd>If present, the preceding {{cssxref("<length>")}} or {{cssxref("<percentage>")}} is applied to the element's content box.</dd>
<dt><a id="auto" name="auto"><code>auto</code></a></dt>
<dd>The browser will calculate and select a width for the specified element.</dd>
<dt><a id="fill" name="fill"><code>fill</code></a> {{experimental_inline}}</dt>
<dd>Use the <code>fill-available</code> inline size or <code>fill-available</code> block size, as appropriate to the writing mode.</dd>
<dt><a id="max-content" name="max-content"><code>max-content</code></a> {{experimental_inline}}</dt>
<dd>The intrinsic preferred width.</dd>
<dt><a id="min-content" name="min-content"><code>min-content</code></a> {{experimental_inline}}</dt>
<dd>The intrinsic minimum width.</dd>
<dt><a id="available" name="available"><code>available</code></a> {{experimental_inline}}</dt>
<dd>The containing block width minus horizontal margin, border and padding.</dd>
<dt id="fit-content"><a id="fit-content" name="fit-content"><code>fit-content</code></a> {{experimental_inline}}</dt>
<dd>The larger of:
<ul>
<li>the intrinsic minimum width</li>
<li>the smaller of the intrinsic preferred width and the available width</li>
</ul>
</dd>
</dl>
<h3 id="Formal_syntax">Formal syntax</h3>
{{csssyntax}}
<h2 id="範例">範例</h2>
<h3 id="預設寬度">預設寬度</h3>
<pre class="brush:css">p.goldie {
background: gold;
}</pre>
<pre class="brush:html"><p class="goldie">The Mozilla community produces a lot of great software.</p></pre>
<p>{{EmbedLiveSample('Default_width', '500px', '64px')}}</p>
<h3 id="像素和相對大小">像素和相對大小</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"><div class="px_length">Width measured in px</div>
<div class="em_length">Width measured in em</div></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"><div class="percent">Width in percentage</div></pre>
<p>{{EmbedLiveSample('Percentage', '500px', '64px')}}</p>
<h3 id="內容最大值">內容最大值</h3>
<pre class="brush:css;">p.maxgreen {
background: lightgreen;
width: intrinsic; /* Safari/WebKit uses a non-standard name */
width: -moz-max-content; /* Firefox/Gecko */
width: -webkit-max-content; /* Chrome */
}</pre>
<pre class="brush:html"><p class="maxgreen">The Mozilla community produces a lot of great software.</p></pre>
<p>{{EmbedLiveSample('max-content_2', '500px', '64px')}}</p>
<h3 id="內容最小值">內容最小值</h3>
<pre class="brush:css">p.minblue {
background: lightblue;
width: -moz-min-content; /* Firefox */
width: -webkit-min-content; /* Chrome */
}</pre>
<pre class="brush:html"><p class="minblue">The Mozilla community produces a lot of great software.</p></pre>
<p>{{EmbedLiveSample('min-content_2', '500px', '155px')}}</p>
<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 Box', '#the-width-and-height-properties', 'width')}}</td>
<td>{{Spec2('CSS3 Box')}}</td>
<td>Added the <code>max-content</code>, <code>min-content</code>, <code>available</code>, <code>fit-content</code>, <code>border-box</code>, <code>content-box</code> keywords.</td>
</tr>
<tr>
<td>{{SpecName('CSS3 Transitions', '#animatable-css', 'width')}}</td>
<td>{{Spec2('CSS3 Transitions')}}</td>
<td>Lists <code>width</code> as animatable.</td>
</tr>
<tr>
<td>{{SpecName('CSS2.1', 'visudet.html#the-width-property', 'width')}}</td>
<td>{{Spec2('CSS2.1')}}</td>
<td>Precises on which element it applies to.</td>
</tr>
<tr>
<td>{{SpecName('CSS1', '#width', 'width')}}</td>
<td>{{Spec2('CSS1')}}</td>
<td>Initial definition.</td>
</tr>
<tr>
<td>{{SpecName('CSS3 Sizing', '#width-height-keywords', 'width')}}</td>
<td>{{Spec2('CSS3 Sizing')}}</td>
<td>Adds new sizing keywords for width and height.</td>
</tr>
</tbody>
</table>
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
<p>{{Compat("css.properties.width")}}</p>
<h2 id="參考">參考</h2>
<ul>
<li><a href="/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model">box model</a>, {{cssxref("height")}}, {{cssxref("box-sizing")}}, {{cssxref("min-width")}}, {{cssxref("max-width")}}</li>
</ul>
|