Javascript Style How to - Get background-position of a div's background image








Question

We would like to know how to get background-position of a div's background image.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#div {<!--from  w ww.  j a v a  2s  .c  om-->
  background-image: url('http://www.java2s.com/style/download.png');
  background-repeat: no-repeat;
  width: 50px;
  height: 50px;
  background-position: 10px 10px;
}
</style>
</head>
<body>
  <div id="div"></div>
  <script type='text/javascript'>
var d = document.getElementById('div');
console.log(window.getComputedStyle(d).getPropertyValue("background-position"));

</script>
</body>
</html>

The code above is rendered as follows: