How to get the Width and Height of an Image using JavaScript? Last Updated : 08 Oct, 2024 Comments Improve Suggest changes Like Article Like Report Given an image and the task is to get the width and height of the image using JavaScript. The width and height property is used to display the image width and height. Syntax for width:let width = this.width;Syntax for height:let height = this.height;Example 1: This example selects the image and then uses this.width and this.height method to get the width and height of the image. html <!DOCTYPE html> <html> <head> <title> Get the real width and height of an image using JavaScript </title> </head> <body style="text-align:center;"> <h2 style="color:green;"> GeeksforGeeks </h2> <h2 style="color:purple;"> Getting the real width and height of an image </h2> <input type="file" id="Image" /> <input type="button" value="Find the dimensions" onclick="CheckImageSize()" /> <script> function CheckImageSize() { let image = document.getElementById("Image").files[0]; createReader(image, function (w, h) { alert("Width is: " + w + "pixels, Height is: " + h + "pixels"); }); } function createReader(file, whenReady) { let reader = new FileReader; reader.onload = function (evt) { let image = new Image(); image.onload = function (evt) { let width = this.width; let height = this.height; if (whenReady) whenReady(width, height); }; image.src = evt.target.result; }; reader.readAsDataURL(file); } </script> </body> <html> Output: Example 2: This example display the dimension of an image. It will display the result without using the alert function. Here we will show the result in the same window. html <!DOCTYPE html> <html> <head> <title> Get the real width and height of an image using JavaScript </title> </head> <body style="text-align:center;"> <img id="myImg" src= "https://media.geeksforgeeks.org/wp-content/uploads/20190613121627/download9.png"> <p> Click the button to display the width and height of the image </p> <button onclick="myFunction()">Try it</button> <p>The width of the image in pixels:</p> <p id="geeks"></p> <p>The height of the image in pixels:</p> <p id="gfg"></p> <script> function myFunction() { let x = document.getElementById("myImg").width; let y = document.getElementById("myImg").height; document.getElementById("geeks").innerHTML = x; document.getElementById("gfg").innerHTML = y; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to get the Width and Height of an Image using JavaScript? S SohomPramanick Follow Improve Article Tags : HTML JavaScript-Questions Similar Reads How to calculate Width and Height of the Window using JavaScript ? In this article, we will know to calculate the width & the height of the window using Javascript. Given an HTML document that is running on a Window & we need to find the height and width of the window. The Window innerWidth property is used for returning the width of a windowâs content area 2 min read How to get the div height using JavaScript ? In this article, we will see How to get the div height using Javascript. We can do this by following ways: Using the offsetHeight property.Using the clientHeight property.Using getBoundingClientRect() Method. Method 1: Using the offsetHeight property: The offsetHeight property of an element is a rea 3 min read How to Set Width and Height of an Image using HTML? When adding images to a webpage, it's important to set their dimensions using the height and width attributes in HTML. Without these dimensions, the browser won't know how much space to reserve for the image, which can cause layout shifts that disrupt the viewing experience. Note: Always specify the 2 min read How to get the Element's Actual Width and Height in JavaScript? In JavaScript, it is often necessary to retrieve the actual width and height of an HTML element. This information can be useful in a variety of scenarios, such as setting the size of an element dynamically, centering an element on the page, or determining the size of an element for animation purpose 5 min read How to get the Width of Scroll bar using JavaScript? Given an HTML document, the task is to get the width of the scrollbar using JavaScript. Approach:Create an element (div) containing a scrollbar.OffsetWidth defines the width of an element + scrollbar width.ClientWidth defines the width of an element.So scrollbar can be defined as width = offsetWidth 3 min read Like