Are there any JavaScript or jQuery APIs or methods to get the dimensions of an image on the page?
Assuming, we want to get image dimensions of
// Query after all the elements on the page have loaded.
// Or, use `onload` on a particular element to check if it is loaded.
document.addEventListener('DOMContentLoaded', function () {
var el = document.getElementById("an-img");
console.log({
"naturalWidth": el.naturalWidth, // Only on HTMLImageElement
"naturalHeight": el.naturalHeight, // Only on HTMLImageElement
"offsetWidth": el.offsetWidth,
"offsetHeight": el.offsetHeight
});
Natural Dimensions
el.naturalWidth
and el.naturalHeight
will get us the natural dimensions, the dimensions of the image file.
Layout Dimensions
el.offsetWidth
and el.offsetHeight
will get us the dimensions at which the element is rendered on the document.