--- 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">/* <length> values */ width: 300px; width: 25em; /* <percentage> 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"><length></a></code> 或者百分比值 <code><a href="https://wiki.developer.mozilla.org/zh-CN/docs/Web/CSS/width$edit#%3Cpercentage%3E"><percentage></a></code>。</li> </ul> <h3 id="值">值</h3> <dl> <dt>{{cssxref("<length>")}}</dt> <dd>使用绝对值定义宽度。</dd> <dt>{{cssxref("<percentage>")}}</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, <length-percentage>))</code></dd> </dl> <h3 id="形式化语法">形式化语法</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="像素_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"><div class="px_length">以 px 度量的宽度</div> <div class="em_length">以 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">按照百分比度量的宽度</div></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"><p class="maxgreen">The Mozilla community produces a lot of great software.</p></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"><p class="minblue">The Mozilla community produces a lot of great software.</p></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>