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

{{Compat("css.properties.max-width")}}

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