--- title: width slug: Web/CSS/width tags: - CSS - CSS Property - Layout - Reference - dimensions - recipe:css-property - size - width browser-compat: css.properties.width translation_of: Web/CSS/width ---
{{CSSRef}}
width は CSS のプロパティで、要素の幅を設定します。既定では、このプロパティはコンテンツ領域の幅を設定しますが、 {{cssxref("box-sizing")}} を border-box に設定すると、境界領域の幅を設定します。
{{cssxref("min-width")}} および {{cssxref("max-width")}} プロパティは width を上書きします。
/* <length> 値 */ width: 300px; width: 25em; /* <percentage> 値 */ width: 75%; /* キーワード値 */ width: max-content; width: min-content; width: fit-content(20em); width: auto; /* グローバル値 */ width: inherit; width: initial; width: revert; width: unset;
automax-contentmin-contentfit-content({{cssxref("<length-percentage>")}})min(max-content, max(min-content, <length-percentage>)) です。ページを拡大してテキストサイズを大きくしたときに、 width を設定した要素が切り捨てられたり、他のコンテンツが見えなくなったりしないようにしてください。
{{CSSInfo}}
p.goldie {
background: gold;
}
<p class="goldie">The Mozilla community produces a lot of great software.</p>
{{EmbedLiveSample('Default_width', '500px', '64px')}}
.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>
{{EmbedLiveSample('Pixels_and_ems', '500px', '64px')}}
.percent {
width: 20%;
background-color: silver;
border: 1px solid red;
}
<div class="percent">Width in percentage</div>
{{EmbedLiveSample('Percentage', '500px', '64px')}}
p.maxgreen {
background: lightgreen;
width: intrinsic; /* Safari/WebKit uses a non-standard name */
width: -moz-max-content; /* Firefox/Gecko */
width: -webkit-max-content; /* Chrome */
width: max-content;
}
<p class="maxgreen">The Mozilla community produces a lot of great software.</p>
{{EmbedLiveSample('max-content', '500px', '64px')}}
p.minblue {
background: lightblue;
width: -moz-min-content; /* Firefox */
width: -webkit-min-content; /* Chrome */
width: min-content;
}
<p class="minblue">The Mozilla community produces a lot of great software.</p>
{{EmbedLiveSample('min-content', '500px', '155px')}}
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName('CSS4 Sizing', '#width-height-keywords', 'width')}} | {{Spec2('CSS4 Sizing')}} | |
| {{SpecName('CSS3 Sizing', '#width-height-keywords', 'width')}} | {{Spec2('CSS3 Sizing')}} | キーワード max-content, min-content, fit-content を追加 |
| {{SpecName('CSS2.1', 'visudet.html#the-width-property', 'width')}} | {{Spec2('CSS2.1')}} | 適用対象の要素を詳しく記載 |
| {{SpecName('CSS1', '#width', 'width')}} | {{Spec2('CSS1')}} | 初回定義 |
{{Compat}}