Style backgroundImageプロパティ

Style 对象参考手册Style オブジェクト

定義と用法

backgroundImage プロパティは、要素の背景画像を設定または返します。

構文

backgroundImage プロパティを設定:

Object.style.backgroundImage="url('URL')|none|inherit"

backgroundImage プロパティを返す:

Object.style.backgroundImage

值 説明
url('URL') 画像ファイルの場所。
none デフォルト。背景画像なし。
inherit background-image プロパティの設定は親要素から継承されます。


ブラウザサポート

Internet ExplorerFirefoxOperaGoogle ChromeSafari

すべての主要ブラウザは backgroundImage プロパティをサポートしています。

注意:IE7 およびそれ以前のバージョンは "inherit" 値をサポートしていません。IE8 は !DOCTYPE が指定されている場合のみ "inherit" をサポートします。IE9 は "inherit" をサポートしています。


ヒントと注記

ヒント:背景画像に加えて、背景色も指定してください。画像が利用できない場合は、背景色が使用されます。


例

例

背景画像を設定:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>サンプル(example.com)</title>
<script>
function displayResult(){
    document.body.style.backgroundColor="#f3f3f3";
    document.body.style.backgroundImage="url('img_tree.png')";
}
</script>
</head>
<body>

<h1>Hello World!</h1>
<br>
<button type="button" onclick="displayResult()">背景画像を設定</button>

</body>
</html>

試してみる »


Style 对象参考手册Style オブジェクト その他の拡張