Style backgroundプロパティ
Style オブジェクト
定義と使い方
background プロパティは、ショートハンド形式で最大5つの独立した背景プロパティを設定または返します。
このプロパティを使用すると、次のものを設定/返すことができます:
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
上記のプロパティに加えて、個々のスタイルプロパティを設定することもできます。個々のプロパティを使用するとより制御しやすくなるため、強く推奨されます。
構文
background プロパティを設定する:
Object.style.background="color image
repeat attachment position"
background プロパティを返す:
Object.style.background
ヒント:background プロパティのデフォルト値は、transparent none repeat scroll 0% 0% です。
| パラメータ | 説明 |
|---|---|
| color | 要素の背景色を設定します |
| image | 要素の背景画像を設定します |
| repeat | 背景画像の繰り返し方法を設定します |
| attachment | 背景画像を固定するか、ページの残りの部分と一緒にスクロールするかを設定します |
| position | 背景画像の開始位置を設定します |
ブラウザサポート
![]()
![]()
![]()
![]()
![]()
すべての主要ブラウザは、background プロパティをサポートしています。
例
例
ドキュメント背景のスタイルを設定する:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(example.com)</title>
<script>
function displayResult(){
document.body.style.background="#f3f3f3 url('img_tree.png') no-repeat right top";
}
</script>
</head>
<body>
<h1>Hello World!</h1>
<br>
<button type="button" onclick="displayResult()">背景を設定</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(example.com)</title>
<script>
function displayResult(){
document.body.style.background="#f3f3f3 url('img_tree.png') no-repeat right top";
}
</script>
</head>
<body>
<h1>Hello World!</h1>
<br>
<button type="button" onclick="displayResult()">背景を設定</button>
</body>
</html>
試してみる »
Style オブジェクト その他の拡張機能