width
Baseline 廣泛可用 *
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-width 和 max-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 的元素不會被截斷和/或不會遮擋其他內容。
正式定義
正式語法
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 |
瀏覽器相容性
載入中…