CSS幅プロパティ
例
3つの<div>要素の幅を設定します。
div.a {
width: auto;
border: 1px
solid black;
}
div.b {
width: 150px;
border: 1px solid black;
}
div.c {
width:
50%;
border: 1px solid black;
}
以下の「自分で試してみてください」の例をもっと見てください。
定義と使用法
プロパティは、要素のwidth
幅を設定します。
要素の幅には、パディング、境界線、またはマージンは含まれません。
注: min - widthプロパティと
max-widthプロパティは、プロパティをオーバーライドしwidth
ます。
デフォルト値: | 自動 |
---|---|
遺伝性の: | いいえ |
アニメート可能: | はい。アニメート可能について読む |
バージョン: | CSS1 |
JavaScript構文: | object .style.width = "500px" |
ブラウザのサポート
表の数字は、プロパティを完全にサポートする最初のブラウザバージョンを示しています。
Property | |||||
---|---|---|---|---|---|
width | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
CSS構文
width: auto|value|initial|inherit;
プロパティ値
Value | Description | Play it |
---|---|---|
auto | Default value. The browser calculates the width | |
length | Defines the width in px, cm, etc. Read about length units | |
% | Defines the width in percent of the containing block | |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
その他の例
例
パーセント値を使用して<img>要素の幅を設定します。
img {
width: 50%;
}
例
<input type = "text">要素の幅を100pxに設定します。ただし、フォーカスが合ったら、幅を250ピクセルにします。
input[type=text] {
width: 100px;
}
input[type=text]:focus {
width: 250px;
}
関連ページ
CSSチュートリアル:CSSの高さと幅
CSSチュートリアル:CSSボックスモデル
CSSリファレンス:heightプロパティ
HTML DOMリファレンス:widthプロパティ