width

Baseline 广泛可用 *

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

* 此特性的某些部分可能存在不同级别的支持。

width CSS 属性用于设置元素的宽度。默认情况下,它设置的是内容区域的宽度,但如果 box-sizing 设置为 border-box,则设置的是边框区域的宽度。

试一试

width: 150px;
width: 20em;
width: 75%;
width: auto;
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    This is a box where you can change the width.
  </div>
</section>
#example-element {
  display: flex;
  flex-direction: column;
  background-color: #5b6dcd;
  height: 80%;
  justify-content: center;
  color: white;
}

只要 width 的值保持在 min-widthmax-width 定义的值范围内,指定的 width 值就适用于内容区域。

  • 如果 width 的值小于 min-width 的值,则 min-width 会覆盖 width
  • 如果 width 的值大于 max-width 的值,则 max-width 会覆盖 width

注意:作为一个几何属性,width 也适用于 <svg><rect><image><foreignObject> 等 SVG 元素,其中 auto<svg> 解析为 100%,对其他元素解析为 0;百分比值对于 <rect> 而言是相对于 SVG 视口宽度的。CSS width 属性值会覆盖 SVG 元素上设置的任何 SVG width 属性值。

语法

css
/* <length> values */
width: 300px;
width: 25em;
width: anchor-size(width);
width: anchor-size(--my-anchor inline, 120%);

/* <percentage> value */
width: 75%;

/* Keyword values */
width: max-content;
width: min-content;
width: fit-content;
width: fit-content(20em);
width: auto;
width: stretch;

/* Global values */
width: inherit;
width: initial;
width: revert;
width: revert-layer;
width: unset;

<length>

将宽度定义为距离值。

<percentage>

将宽度定义为包含块宽度的百分比。

auto

浏览器将为指定元素计算并选择宽度。

max-content

固有的首选宽度。

min-content

固有的最小宽度。

fit-content

使用可用空间,但不超过 max-content,即 min(max-content, max(min-content, stretch))

fit-content(<length-percentage>)

使用 fit-content 公式,其中可用空间被指定参数替换,即 min(max-content, max(min-content, <length-percentage>))

stretch

将元素的外边距框宽度设置为其包含块的宽度。它尝试使外边距框填充包含块中的可用空间,因此在某种程度上类似于 100%,但将结果大小应用于外边距框而不是由 box-sizing 确定的框。

无障碍

确保当页面缩放以增大文本大小时,设置了 width 的元素不会被截断和/或不会遮挡其他内容。

正式定义

初始值auto
应用于除非替换行内元素、表格行和行组之外的所有元素。
继承性
百分比参照包含块的宽度
计算值百分比或 auto 或绝对长度。
动画类型一个长度百分比或 calc();

正式语法

width = 
auto |
<length-percentage [0,∞]> |
min-content |
max-content |
fit-content( <length-percentage [0,∞]> ) |
<calc-size()> |
<anchor-size()> |
stretch |
fit-content |
contain

<length-percentage> =
<length> |
<percentage>

<calc-size()> =
calc-size( <calc-size-basis> , <calc-sum> )

<anchor-size()> =
anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )

<calc-size-basis> =
<size-keyword> |
<calc-size()> |
any |
<calc-sum>

<calc-sum> =
<calc-product> [ [ '+' | '-' ] <calc-product> ]*

<anchor-name> =
<dashed-ident>

<anchor-size> =
width |
height |
block |
inline |
self-block |
self-inline

<calc-product> =
<calc-value> [ [ '*' | / ] <calc-value> ]*

<calc-value> =
<number> |
<dimension> |
<percentage> |
<calc-keyword> |
( <calc-sum> )

<calc-keyword> =
e |
pi |
infinity |
-infinity |
NaN

示例

默认宽度

css
p.gold {
  background: gold;
}
html
<p class="gold">The MDN community writes really great documentation.</p>

使用像素和 em 的示例

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;
}
html
<div class="px_length">Width measured in px</div>
<div class="em_length">Width measured in em</div>

百分比示例

css
.percent {
  width: 20%;
  background-color: silver;
  border: 1px solid red;
}
html
<div class="percent">Width in percentage</div>

使用“max-content”的示例

css
p.max-green {
  background: lightgreen;
  width: max-content;
}
html
<p class="max-green">The MDN community writes really great documentation.</p>

使用“min-content”的示例

css
p.min-blue {
  background: lightblue;
  width: min-content;
}
html
<p class="min-blue">The MDN community writes really great documentation.</p>

拉伸宽度以填充包含块

HTML

html
<div class="parent">
  <div class="child">text</div>
</div>

<div class="parent">
  <div class="child stretch">stretch</div>
</div>

CSS

css
.parent {
  border: solid;
  margin: 1rem;
  display: flex;
}

.child {
  background: #00999999;
  margin: 1rem;
}

.stretch {
  width: stretch;
}

结果

规范

规范
CSS Box Sizing Module Level 4
# sizing-values

浏览器兼容性

另见