スタイルbackgroundColorプロパティ
例
ドキュメントの背景色を設定します。
document.body.style.backgroundColor = "red";
以下の「自分で試してみてください」の例をもっと見てください。
定義と使用法
backgroundColorプロパティは、要素の背景色を設定または返します。
ブラウザのサポート
表の数字は、プロパティを完全にサポートする最初のブラウザバージョンを示しています。
Property | |||||
---|---|---|---|---|---|
backgroundColor | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
構文
backgroundColorプロパティを返します。
object.style.backgroundColor
backgroundColorプロパティを設定します。
object.style.backgroundColor = "color|transparent|initial|inherit"
プロパティ値
Value | Description |
---|---|
color | Specifies the background color. Look at CSS Color Values for a complete list of possible color values |
transparent | Default. The background color is transparent (underlying content will shine through) |
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.backgroundColor = "lightblue";
例
特定の<div>要素の背景色を返します。
alert(document.getElementById("myDiv").style.backgroundColor);
例
ドキュメントの背景色を返します。
alert(document.body.style.backgroundColor);
関連ページ
CSSチュートリアル:CSSの背景
CSSリファレンス:background-colorプロパティ
HTML DOMリファレンス:背景プロパティ