JavaScript Image naturalWidth

plus2net.com tutorial site

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

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

Natural vs rendered width Top ↑

If CSS or JavaScript changes the displayed size, naturalWidth still reports the intrinsic width 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.naturalWidth);
});

Reset to natural dimensions Top ↑

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

Working demos Top ↑

Demo resetting image to naturalWidth / natural size

Demo changing height and width

Demo resetting to naturalHeight and naturalWidth

Reading naturalHeight 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