---
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>