スタイル幅プロパティ
例
<button>要素の幅を設定します。
document.getElementById("myBtn").style.width = "300px";
定義と使用法
widthプロパティは、要素の幅を設定または返します。
widthプロパティは、ブロックレベルの要素、または絶対位置または固定位置の要素にのみ影響します。オーバーフローしたコンテンツは、overflowプロパティを使用して操作できます。
ヒント:height プロパティを使用して、要素の高さを設定または返します。
ブラウザのサポート
Property | |||||
---|---|---|---|---|---|
width | Yes | Yes | Yes | Yes | Yes |
構文
widthプロパティを返します。
object.style.width
widthプロパティを設定します。
object.style.width = "auto|length|%|initial|inherit"
プロパティ値
Value | Description |
---|---|
auto | The browser sets the width. This is default |
length | Defines the width in length units |
% | Defines the width in % of the parent element |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
技術的な詳細
デフォルト値: | 自動 |
---|---|
戻り値: | 要素の幅を表す文字列 |
CSSバージョン | CSS1 |
その他の例
例
<div>要素の幅を変更します。
document.getElementById("myDIV").style.width = "500px";
例
<img>要素の高さと幅を変更します。
document.getElementById("myImg").style.height = "300px";
document.getElementById("myImg").style.width = "300px";
例
<img>要素の幅を返します。
alert(document.getElementById("myImg").style.width);
関連ページ
CSSチュートリアル:CSSディメンション
CSSチュートリアル:CSSボックスモデル
CSSリファレンス:widthプロパティ
❮スタイルオブジェクト