Latest web development tutorials

HTML DOM Style backgroundPosition property

Style Object Reference Style Objects

Definition and Usage

backgroundPosition property sets or returns the position of the background image elements.

grammar

Setting backgroundPosition properties:

Object.style.backgroundPosition="position"

Back backgroundPosition properties:

Object.style.backgroundPosition

Tip: The default value of the property is backgroundPosition: 0% 0%.

描述
top left
top center
top right
center left
center center
center right
bottom left
bottom center
bottom right
如果仅指定一个关键字,其他值将是 "center"。
x% y% x 值表示水平位置,y 值表示垂直位置。左上角是 0% 0%。右下角是 100% 100%。如果仅指定一个值,其他值将是 50%。
xpos ypos x 值表示水平位置,y 值表示垂直位置。左上角是 0 0。单位可以是像素(0px 0px)或任何其他的 CSS 单位。如果仅指定一个值,其他值将是 50%。您可以混合使用 % 和单位。
inherit 背景位置属性的设置从父元素继承。


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support backgroundPosition property.

Note: IE7 and earlier versions do not support the "inherit" value.IE8 only provides! DOCTYPE supported "inherit". IE9 support "inherit".


Examples

Examples

Change the background image here:

<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "utf-8">
<Title> This tutorial (w3big.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 ()"> to modify the background image position </ button>
<br>
<Div id = "div1">
</ Div>

</ Body>
</ Html>

try it"


Style Object Reference Style Objects