スタイルbackgroundPositionプロパティ
例
背景画像の位置を変更します。
document.body.style.backgroundPosition = "top right";
以下の「自分で試してみてください」の例をもっと見てください。
定義と使用法
backgroundPositionプロパティは、要素内のbackground-imageの位置を設定または返します。
ブラウザのサポート
表の数字は、プロパティを完全にサポートする最初のブラウザバージョンを示しています。
Property | |||||
---|---|---|---|---|---|
backgroundPosition | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
構文
backgroundPositionプロパティを返します。
object.style.backgroundPosition
backgroundPositionプロパティを設定します。
object.style.backgroundPosition = value
プロパティ値
Value | Description |
---|---|
top left top center top right center left center center center right bottom left bottom center bottom right |
If you only specify one keyword, the other value will be "center". |
x% y% | The x value indicates the horizontal position and the y value indicates the vertical position. The top left corner is 0% 0%. The right bottom corner is 100% 100%. If you only specify one value, the other value will be 50%. |
xpos ypos | The x value indicates the horizontal position and the y value indicates the vertical position. The top left corner is 0 0. Units can be pixels (0px 0px) or any other CSS units. If you only specify one value, the other value will be 50%. You can mix % and units |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
技術的な詳細
デフォルト値: | 0%0% |
---|---|
戻り値: | 背景画像の位置を表す文字列 |
CSSバージョン | CSS1 |
その他の例
例
<div>要素の背景画像の位置を中央下に変更します。
document.getElementById("myDiv").style.backgroundPosition = "center bottom";
例
<div>要素の背景画像の位置を水平方向に200ピクセル、垂直方向に40ピクセルに変更します。
document.getElementById("myDiv").style.backgroundPosition = "200px 40px";
例
<div>要素の背景画像の位置を返します。
document.getElementById("myDiv").style.backgroundPosition;
関連ページ
CSSチュートリアル:CSSの背景
CSSリファレンス:background-imageプロパティ
CSSリファレンス:background-positionプロパティ
HTML DOMリファレンス:背景プロパティ