width
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
* Some parts of this feature may have varying levels of support.
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: #ffffff;
}
width に指定された値は、その値が min-width および max-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 要素に設定された width 属性の値を上書きします。
構文
/* <length> 値 */
width: 300px;
width: 25em;
width: anchor-size(--myAnchor inline, 120%);
width: minmax(100px, anchor-size(width));
/* <percentage> 値 */
width: 75%;
/* キーワード値 */
width: max-content;
width: min-content;
width: fit-content;
width: fit-content(20em);
width: auto;
width: stretch;
/* グローバル値 */
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 で決定されるボックスではなく、マージンボックスに適用されます。メモ: ブラウザーが
stretch値のために使用する別名と実装状況については、ブラウザーの互換性の節を参照してください。
アクセシビリティ
ページを拡大してテキストサイズを大きくしたときに、 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
例
>既定の幅
p.goldie {
background: gold;
}
<p class="goldie">The MDN community writes really great documentation.</p>
ピクセル数と em 単位
.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;
}
<div class="px_length">Width measured in px</div>
<div class="em_length">Width measured in em</div>
パーセント値の例
.percent {
width: 20%;
background-color: silver;
border: 1px solid red;
}
<div class="percent">Width in percentage</div>
max-content の例
p.max-green {
background: lightgreen;
width: max-content;
}
<p class="max-green">The MDN community writes really great documentation.</p>
min-content の例
p.min-blue {
background: lightblue;
width: min-content;
}
<p class="min-blue">The MDN community writes really great documentation.</p>
仕様書
| Specification |
|---|
| CSS Box Sizing Module Level 4> # sizing-values> |
ブラウザーの互換性
Loading…