aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/max-width/index.html
blob: c059156cad42d6bc3c5066cc6e93e00947d9df05 (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
203
204
205
206
207
208
209
210
211
212
213
214
---
title: max-width
slug: Web/CSS/max-width
translation_of: Web/CSS/max-width
---
<p>{{ CSSRef() }}</p>

<h2 id="摘要">摘要</h2>

<p>max-width 属性用来给元素设置最大宽度值. 定义了max-width的元素会在达到max-width值之后避免进一步按照width属性设置变大.</p>

<p>{{ Cssxref("max-width") }} 会覆盖{{cssxref("width")}}设置, 但 {{ Cssxref("min-width") }}设置会覆盖 {{ Cssxref("max-width") }}.</p>

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

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

<pre class="brush:css">/* &lt;长度&gt; 值 */
max-width: 3.5em;

/* &lt;百分比&gt; 值 */
max-width: 75%;

/* 关键字 */
max-width: none;
max-width: max-content;
max-width: min-content;
max-width: fit-content;
max-width: fill-available;

/* 全局设置 */
max-width: inherit;
max-width: initial;
max-width: unset;
</pre>

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

<dl>
 <dt><code>none</code></dt>
 <dd>元素未设置最大值</dd>
 <dt><code>&lt;长度&gt;</code></dt>
 <dd>请参阅{{cssxref("&lt;length&gt;")}}所提及的长度单位.</dd>
 <dt><code>&lt;百分比&gt;</code></dt>
 <dd>以父级块级容器宽度的百分比{{cssxref("&lt;percentage&gt;")}}作为最大宽度.</dd>
 <dt><code>max-content</code>{{experimental_inline()}}</dt>
 <dd>The intrinsic preferred width.</dd>
 <dt><code>min-content</code>{{experimental_inline()}}</dt>
 <dd>The intrinsic minimum width.</dd>
 <dt><code>fill-available</code>{{experimental_inline()}}</dt>
 <dd>The containing block width minus horizontal margin, border, and padding. Some browsers implement an ancient name for this keyword, <code>available</code>.</dd>
 <dt><code>fit-content</code>{{experimental_inline()}}</dt>
 <dd><code>max-content等价.</code></dd>
</dl>

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

{{csssyntax}}

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

<p>在下面的例子里,id为"child"的&lt;div&gt;元素设置了width值, 使它的宽度与父元素"parent"相等(100%),但是max-width值限制了它最大宽度只能到150px.</p>

<div id="basic-max-width-demo">
<pre class="brush: html">&lt;div id="parent"&gt;
    &lt;div id="child"&gt;
        The Mozilla community produces a lot of great software.
    &lt;/div&gt;
&lt;/div&gt;
</pre>

<pre class="brush: css">#parent { width: 300px; }

#child  { background: gold;
          width: 100%;
          max-width: 150px;
        }
</pre>
</div>

<p>{{EmbedLiveSample("basic-max-width-demo", 350, 100)}}</p>

<p>fit-content 值可以用来给元素设置基于其内容大小的宽度:</p>

<div id="fit-content-demo">
<pre class="brush: html" style="display: none;">&lt;div id="parent"&gt;
    &lt;div id="child"&gt;
        Child Text
    &lt;/div&gt;
&lt;/div&gt;
</pre>

<pre class="brush: css">#parent { background: lightblue;
          width: 300px; }

#child  { background: gold;
          width: 100%;
          max-width: -moz-fit-content;
          max-width: -webkit-fit-content;
        }
</pre>
</div>

<p>{{EmbedLiveSample("fit-content-demo", 400, 100)}}</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 Sizing', '#width-height-keywords', 'max-width') }}</td>
   <td>{{ Spec2('CSS3 Sizing') }}</td>
   <td>Adds the <code>max-content</code>, <code>min-content</code>, <code>fit-content</code>, and <code>fill-available</code> keywords.<br>
    <em>Both CSS3 Box and CSS3 Writing Modes drafts defined at some point these keywords. These drafts are superseded by this spec.</em></td>
  </tr>
  <tr>
   <td>{{ SpecName('CSS3 Transitions', '#animatable-css', 'max-width') }}</td>
   <td>{{ Spec2('CSS3 Transitions') }}</td>
   <td>Defines <code>max-width</code> as animatable.</td>
  </tr>
  <tr>
   <td>{{ SpecName('CSS2.1', 'visudet.html#min-max-widths', 'max-width') }}</td>
   <td>{{ Spec2('CSS2.1') }}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

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

<p>{{ CompatibilityTable() }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>{{CompatGeckoDesktop("1.0")}}</td>
   <td>7.0</td>
   <td>4.0</td>
   <td>2.0.2 (416),<br>
    buggy before</td>
  </tr>
  <tr>
   <td>applies to <code>&lt;table&gt; </code>[1]</td>
   <td>{{ CompatNo() }}</td>
   <td>{{CompatVersionUnknown()}}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{CompatVersionUnknown()}}</td>
   <td>{{ CompatNo() }}</td>
  </tr>
  <tr>
   <td rowspan="1"><code>max-content</code>, <code>min-content</code>, <code>fit-content</code>, and <code>fill-</code><code>available</code> {{ experimental_inline() }}</td>
   <td>{{ CompatNo() }} [3]</td>
   <td>{{CompatGeckoDesktop("1.9")}}{{property_prefix("-moz")}} [2]</td>
   <td rowspan="1">{{ CompatNo() }}</td>
   <td rowspan="1">{{ CompatNo() }}</td>
   <td>{{ CompatNo() }} [3]</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] CSS 2.1 explicitly leaves the behavior of <code>max-width</code> with {{ HTMLElement("table") }} undefined. Therefore any behavior is CSS2.1-compliant; newer CSS specifications may define this behavior, so Web developers shouldn't rely on a specific one now.</p>

<p>[2] Gecko experimentally implements the definitions given in CSS3 Basic Box. This one defines <code>available</code> and not <code>fill-available</code>. Also the definition of <code>fit-content</code> is simpler than in CSS3 Sizing.</p>

<p>[3] WebKit implements an earlier proposal for setting the height to an intrinsic height: the keywords <code>intrinsic</code>, instead of <code>max-content</code>, and <code>min-intrinsic</code>, instead of <code>min-content</code> There is no equivalent for <code>fill-available</code> or <code>fit-content</code>.</p>

<h2 id="参考链接">参考链接</h2>

<ul>
 <li>{{ Cssxref("width") }}, {{ Cssxref("min-width") }}, {{ Cssxref("max-height") }}</li>
 <li><a href="/en/CSS/box_model" title="en/CSS/box_model">The box model</a>, {{ Cssxref("box-sizing") }}</li>
</ul>