JavaScript

How to get image size (height & width) using JavaScript

You can use JavaScript’s clientWidth and clientHeight properties to get the current width and height of an image. These properties will round the value to an integer.
 

How to get image size (height & width) using JavaScript
<!DOCTYPE html>
<html>
  <head>
  <title>Get image size (height / width) using JavaScript</title>
  <script type="text/javascript">
    function getSize(){
      var img = document.querySelector("#myImg");
      var width = img.clientWidth;
      var height = img.clientHeight;
      alert("Width =" + width + ", " + "Height =" + height);
    }
  </script>
  </head>
  <body>
    <img src="https://via.placeholder.com/300" id="myImg">
    <button type="button" onclick="getSize();">Get image size</button>
  </body>
</html>
Result
Get image size (height / width) using JavaScript

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *