Style backgroundPositionProperty

Style 对象参考手册Style Object

Definition and Usage

The backgroundPosition property sets or returns the position of the element's background image.

Syntax

Set the backgroundPosition property:

Object.style.backgroundPosition="position"

Return the backgroundPosition property:

Object.style.backgroundPosition

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

Value Description
top left
top center
top right
center left
center center
center right
bottom left
bottom center
bottom right
If only one keyword is specified, the other value will be "center".
x% y% The x value represents the horizontal position, and the y value represents the vertical position. The top-left corner is 0% 0%. The bottom-right corner is 100% 100%. If only one value is specified, the other value will be 50%.
xpos ypos The x value represents the horizontal position, and the y value represents the vertical position. The top-left corner is 0 0. Units can be pixels (0px 0px) or any other CSS units. If only one value is specified, the other value will be 50%. You can mix % and units.
inherit The setting of the background-position property is inherited from the parent element.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the backgroundPosition property.

Note:IE7 and earlier versions do not support the "inherit" value. IE8 supports "inherit" only if a !DOCTYPE is specified. IE9 supports "inherit".


Example

Example

Change the position of the background image:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Tutorial(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()">Modify background image position</button>
<br>
<div id="div1">
</div>

</body>
</html>

Try it Yourself »


Style 对象参考手册Style Object Other Extensions