JavaScript Image naturalHeight

plus2net.com tutorial site

naturalHeight is a read-only image property that reports the intrinsic, density-corrected height of the loaded image in CSS pixels. It is different from the rendered height, which can change because of attributes, CSS or JavaScript.

const value = document.getElementById('i1').naturalHeight;
Table of Contents

Natural vs rendered height Top ↑

If CSS or JavaScript changes the displayed size, naturalHeight still reports the intrinsic height of the loaded image. If the intrinsic size is not yet available, it can be 0.

Read it after the image loads Top ↑

const image = document.getElementById('i1');
image.addEventListener('load', () => {
  console.log(image.naturalHeight);
});

Reset to natural dimensions Top ↑

image.width = image.naturalWidth;
image.height = image.naturalHeight;

Working demos Top ↑

Demo resetting image to naturalHeight / natural size

Demo changing height and width

Demo resetting to naturalHeight and naturalWidth

Reading naturalWidth of Image Image Object




Subscribe to our YouTube Channel here



plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer