Style backgroundPositionプロパティ
Style オブジェクト
定義と使い方
backgroundPosition プロパティは、要素の背景画像の位置を設定または取得します。
構文
backgroundPosition プロパティの設定:
Object.style.backgroundPosition="position"
backgroundPosition プロパティの取得:
Object.style.backgroundPosition
ヒント:backgroundPosition プロパティのデフォルト値は:0% 0% です。
| 值 | 説明 |
|---|---|
| top left top center top right center left center center center right bottom left bottom center bottom right |
キーワードを1つだけ指定した場合、他の値は "center" になります。 |
| x% y% | x 値は水平位置、y 値は垂直位置を表します。左上は 0% 0% です。右下は 100% 100% です。値を1つだけ指定した場合、他の値は 50% になります。 |
| xpos ypos | x 値は水平位置、y 値は垂直位置を表します。左上は 0 0 です。単位はピクセル(0px 0px)または他の任意の CSS 単位を使用できます。値を1つだけ指定した場合、他の値は 50% になります。% と単位を混在させることができます。 |
| inherit | 背景位置プロパティの設定は親要素から継承されます。 |
ブラウザサポート
![]()
![]()
![]()
![]()
![]()
すべての主要ブラウザが backgroundPosition プロパティをサポートしています。
注記:IE7 およびそれ以前のバージョンは "inherit" 値をサポートしていません。IE8 は !DOCTYPE が指定されている場合のみ "inherit" をサポートします。IE9 は "inherit" をサポートしています。
例
例
背景画像の位置を変更する:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>サンプル(example.com)</title>
<style>
div{
background-image: url('img_tree.png');
background-repeat: no-repeat;
width: 400px;
height: 400px;
border: 1px solid #000000;
}
</style>
<script>
function displayResult(){
document.getElementById("div1").style.backgroundPosition="center bottom";
}
</script>
</head>
<body>
<button type="button" onclick="displayResult()">背景画像の位置を変更</button>
<br>
<div id="div1">
</div>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>サンプル(example.com)</title>
<style>
div{
background-image: url('img_tree.png');
background-repeat: no-repeat;
width: 400px;
height: 400px;
border: 1px solid #000000;
}
</style>
<script>
function displayResult(){
document.getElementById("div1").style.backgroundPosition="center bottom";
}
</script>
</head>
<body>
<button type="button" onclick="displayResult()">背景画像の位置を変更</button>
<br>
<div id="div1">
</div>
</body>
</html>
試してみる »
Style オブジェクト その他の拡張