Javascript DOM HTML Image width Property get width styled by CSS

Introduction

Return the width of an image that has been styled with CSS:

var x = document.getElementById("myImg").width;

Click the button to return the width of the image.

View in separate window

<!DOCTYPE html>
<html>
<body>

<img id="myImg" src="image1.png" style="width:100px;height:160px;">
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//w  ww. j  ava2 s .c  om
  var x = document.getElementById("myImg").width;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related