Change the position of the background-image in a <div> element to 200px horizontal and 40px vertical: - Javascript CSS Style Property

Javascript examples for CSS Style Property:backgroundPosition

Description

Change the position of the background-image in a <div> element to 200px horizontal and 40px vertical:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {/*from  w w w  .j  a va  2 s  .  co  m*/
    background-image: url('http://java2s.com/resources/a.png');
    background-repeat: no-repeat;
    width: 400px;
    height: 400px;
    border: 1px solid #000000;
}
</style>
</head>
<body>

<button type="button" onclick="myFunction()">Position background image</button>
<br>
<div id="myDiv">
</div>

<script>
function myFunction() {
    document.getElementById("myDiv").style.backgroundPosition = "200px 40px";
}
</script>

</body>
</html>

Related Tutorials