-1

I have an image tag:

HTML code:

<img src="image.jpg"></img>

CSS code:

img
{  
   max-width: 100%;
   max-height: 100%; 
}

I just want to know it's actual height and width in pixels(size in memory), if it's width and height is set to auto. How to do that in javascript?

alyssaeliyah
  • 2,214
  • 6
  • 33
  • 80

3 Answers3

0

One way would be doing it like this. This way you get the current height and width of the DOM-element.

<img id="someId" src="image.jpg"></img>

var imageElement = document.getElementById('someId'); 
var height = imageElement.clientHeight;
var width = imageElement.clientWidth;
Leakim
  • 626
  • 1
  • 10
  • 19
0

clientWidth and clientHeight are DOM properties that show the current in-browser size of the inner dimensions of a DOM element (excluding margin and border). So in the case of an img element, this will get the actual dimensions of the visible image.

var img = document.getElementById('imageid');
 //or however you get a handle to the IMG
 var width = img.clientWidth;
 var height = img.clientHeight;
Sachin
  • 737
  • 1
  • 9
  • 23
Naga
  • 1
  • 2
0

You can use JavaScript to gather the element

Use for height document.querySelector('img').offsetHeight;

Use for width document.querySelector('img').offsetWidth;

Usually people set an id or a class on the element in order to easily grab it.

like this <img class='image' src='image.jpg'></img>

now you can grab the element by its classname

document.querySelector('.image').offsetHeight;

David Snabel
  • 9,801
  • 6
  • 21
  • 29