スタイルbackgroundImageプロパティ
例
ドキュメントの背景画像を設定します。
document.body.style.backgroundImage = "url('img_tree.png')";
以下の「自分で試してみてください」の例をもっと見てください。
定義と使用法
backgroundImageプロパティは、要素の背景画像を設定または返します。
ヒント: background-imageに加えて、background-colorも指定する必要があります。画像が利用できない場合は、背景色が使用されます。
ブラウザのサポート
表の数字は、プロパティを完全にサポートする最初のブラウザバージョンを示しています。
Property | |||||
---|---|---|---|---|---|
backgroundImage | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
構文
backgroundImageプロパティを返します。
object.style.backgroundImage
backgroundImageプロパティを設定します。
object.style.backgroundImage = "url('URL')|none|initial|inherit"
プロパティ値
Value | Description |
---|---|
url('URL') | The location of the image file |
none | No background image. This is default |
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.backgroundImage = "url('img_tree.png')";
例
特定の<div>要素の背景画像を返します。
alert(document.getElementById("myDiv").style.backgroundImage);
例
ドキュメントの背景画像を返します。
alert(document.body.style.backgroundImage);
関連ページ
CSSチュートリアル:CSSの背景
CSS3チュートリアル:CSS3の背景
CSSリファレンス:background-imageプロパティ
HTML DOMリファレンス:背景プロパティ