Testing

naturalWidth & naturalHeight: Browser Support, Use Cases

naturalWidth and naturalHeight work in Chrome 4+, Edge 12+, Firefox 2+, Safari 3.1+, Opera 9+, Samsung Internet 4+, and Internet Explorer 9-11. See full support.

Author

Prince Dewani

May 6, 2026

naturalWidth and naturalHeight are read-only HTMLImageElement properties from the WHATWG HTML Standard that return an image's intrinsic dimensions in CSS pixels. Both work in Chrome 4+, Edge 12+, Firefox 2+, Safari 3.1+ macOS, Safari 3.2+ iOS, Opera 9+, Samsung Internet 4+, and Internet Explorer 9 to 11, while IE 5.5 to 8 never added support.

This guide covers what they are, the browsers that support them, how to use them in JavaScript, the difference from width, and the common pitfalls.

What are naturalWidth and naturalHeight?

naturalWidth and naturalHeight are read-only properties of the HTMLImageElement interface defined by the WHATWG HTML Standard. They return the intrinsic, density-corrected width and height of the image source in CSS pixels. The values are 0 until the browser finishes decoding the image data.

Which browsers support naturalWidth and naturalHeight?

naturalWidth and naturalHeight are Baseline Widely Available and ship in every modern desktop and mobile browser. Internet Explorer 9 through 11 also support both properties; IE 5.5 to 8 are the only browsers that never added them.

Loading browser compatibility data...

naturalWidth and naturalHeight compatibility in Chrome

Chrome supports naturalWidth and naturalHeight from Chrome 4 on Windows, macOS, Linux, ChromeOS, and Android. The values are corrected for device pixel ratio, so a 2x source on a retina screen still reports its CSS pixel size. Chrome on low-end Android can downgrade image resolution, and naturalWidth then reflects the downgraded pixel size.

naturalWidth and naturalHeight compatibility in Edge

Microsoft Edge supports naturalWidth and naturalHeight from Edge 12 on Windows, macOS, and Linux. Edge Legacy 12 to 18 shipped both properties through the EdgeHTML engine, and Chromium-based Edge 79 onward inherits the same support from Blink.

naturalWidth and naturalHeight compatibility in Firefox

Firefox supports naturalWidth and naturalHeight from Firefox 2 on Windows, macOS, Linux, and Android. Gecko reports 0 for SVG sources that carry only a viewBox without explicit width and height attributes, which matches the WHATWG specification but trips up authors who expect a pixel value.

naturalWidth and naturalHeight compatibility in Safari

Safari supports naturalWidth and naturalHeight from Safari 3.1 on macOS and Safari 3.2 on iOS and iPadOS. Both values are returned in CSS pixels, density-corrected for retina displays. Mobile Safari behaves identically to desktop Safari, including the WebKit handling of broken images, where the values stay at 0 instead of throwing.

naturalWidth and naturalHeight compatibility in Opera

Opera supports naturalWidth and naturalHeight from Opera 9 on Windows, macOS, and Linux. Opera Mobile picks up support from Opera Mobile 10. Both desktop and mobile Opera now ride the Blink engine, so the values match Chrome's behavior on every platform.

naturalWidth and naturalHeight compatibility in Samsung Internet

Samsung Internet supports naturalWidth and naturalHeight from Samsung Internet 4 on Galaxy phones and tablets. The browser inherits Chromium's density correction, so the values match what Chrome on the same Android device would return for the same image source.

naturalWidth and naturalHeight compatibility in Android Browser

The legacy Android Browser supports naturalWidth and naturalHeight from Android Browser 2.1. Modern Chrome for Android continues the same support across every Android version Google still ships. Android Browser 2.0 and earlier did not expose the properties.

naturalWidth and naturalHeight compatibility in Internet Explorer

Internet Explorer 9, 10, and 11 support naturalWidth and naturalHeight on Windows. IE 5.5 through 8 never shipped either property, so reading img.naturalWidth in those builds returns undefined. Microsoft has retired Internet Explorer, so most modern QA pipelines can ignore the IE 5.5 to 8 path entirely.

Note

Note: naturalWidth and naturalHeight have edge cases on SVG sources, broken images, and density-corrected pixels. Test it on real browsers and OS with TestMu AI. Try TestMu AI free!

How do you use naturalWidth and naturalHeight in JavaScript?

Read the properties off any HTMLImageElement after the image has finished decoding. The values are valid inside a load event handler, after the boolean complete property is true, or after an awaited img.decode() call.

  • Grab the element: Use document.querySelector or pass the element directly. Both DOM-attached and detached HTMLImageElement instances expose the properties.
  • Wait for the load event: Attach addEventListener('load', handler). Inside the handler, naturalWidth and naturalHeight are always populated.
  • Guard with the complete property: If the image is already cached, the load event has fired before your script ran. Check img.complete first and read the values immediately when it is true.
  • Read the values: Both return numbers in CSS pixels. Divide naturalWidth by naturalHeight to compute the aspect ratio.
  • Re-read on src changes: Setting img.src resets the values to 0. Re-attach the load handler so the layout code sees the new dimensions.
// 1. Read intrinsic size after the image element finishes loading.
const heroImg = document.querySelector('img.hero');

heroImg.addEventListener('load', () => {
    console.log('Intrinsic width:', heroImg.naturalWidth, 'CSS px');
    console.log('Intrinsic height:', heroImg.naturalHeight, 'CSS px');
});

// 2. Preload an image with the Image constructor and read its size off-DOM.
const probe = new Image();
probe.addEventListener('load', () => {
    const aspectRatio = probe.naturalWidth / probe.naturalHeight;
    console.log('Aspect ratio:', aspectRatio.toFixed(3));
});
probe.src = '/static/banner.jpg';

// 3. Guard against the not-yet-loaded case before reading the value.
function getIntrinsicSize(img) {
    if (img.complete && img.naturalWidth > 0) {
        return { width: img.naturalWidth, height: img.naturalHeight };
    }
    return null;
}
...

What is the difference between naturalWidth and width?

naturalWidth and width look interchangeable but they answer different questions. naturalWidth describes the source asset; width describes how the asset is rendered on the page. Same story for naturalHeight versus height.

DimensionnaturalWidth / naturalHeightwidth / height
What it returnsIntrinsic source dimensions in CSS pixelsRendered dimensions on the page in CSS pixels
Density correctionYes, density-corrected for the device pixel ratioYes, but reflects the layout box, not the source
Read or writeRead-onlyWritable; mirrors the HTML width and height attributes
Affected by CSSNo, ignores CSS rules on the elementYes, returns the layout-box size after CSS
Value before load0 until the image finishes decodingThe HTML width attribute, or 0 if unset
Common use caseAspect-ratio math, lazy-loaded layout, image probingSetting or reading the rendered size on the page
...

What are the common pitfalls of naturalWidth and naturalHeight?

Cross-browser support is solid, but the value is empty in more situations than authors expect. Most production bugs trace back to reading the property too early, mis-handling SVGs, or forgetting that the src reset clears the cached value.

  • Reading before the image decodes: The most frequent failure. naturalWidth is 0 until the load event fires. Code that runs in the parser or in a synchronous callback after setting src reads 0 in every browser.
  • SVG with viewBox only: Chrome, Firefox, and Safari return 0 for an SVG that has a viewBox but no width and height attributes on the root element. Add explicit width and height to the SVG, or measure with getBoundingClientRect on the rendered element.
  • Cached images skip the load event: If the browser serves the image from cache before your listener attaches, the load event never fires for that listener. Check img.complete first and read the value synchronously when it is true.
  • Broken images report 0: A 404 or a CORS-blocked image leaves naturalWidth at 0 in every browser. Listen for the error event in parallel with load, and treat 0-valued naturalWidth as "no image" rather than "tiny image."
  • currentSrc versus src on srcset: When the browser picks a different srcset candidate, naturalWidth reflects the chosen source, not the src attribute. Read img.currentSrc to know which file the browser actually decoded.
  • Density mismatch on retina: A 1600 by 1600 raster served as a 2x asset reports 800 in naturalWidth, not 1600. Authors who debug an image-pipeline regression often blame the file on disk; the browser is correctly density-correcting the value.

In my experience, the trap that bites teams hardest is the cached-image case in single-page apps: a tab restore or a soft route change runs the layout code before the listener attaches, and naturalWidth is already populated but the load event never re-fires, so the dependent layout never recalculates.

Citations

All naturalWidth and naturalHeight version numbers and platform notes in this guide come from these primary sources:

Author

Prince Dewani is a Community Contributor at TestMu AI, where he manages content strategies around software testing, QA, and test automation. He is certified in Selenium, Cypress, Playwright, Appium, Automation Testing, and KaneAI. Prince has also presented academic research at the international conference PBCON-01. He further specializes in on-page SEO, bridging marketing with core testing technologies. On LinkedIn, he is followed by 4,300+ QA engineers, developers, DevOps experts, tech leaders, and AI-focused practitioners in the global testing community.

Open in ChatGPT Icon

Open in ChatGPT

Open in Claude Icon

Open in Claude

Open in Perplexity Icon

Open in Perplexity

Open in Grok Icon

Open in Grok

Open in Gemini AI Icon

Open in Gemini AI

Copied to Clipboard!
...

3000+ Browsers. One Platform.

See exactly how your site performs everywhere.

Try it free
...

Write Tests in Plain English with KaneAI

Create, debug, and evolve tests using natural language.

Try for free

Frequently asked questions

Did you find this page helpful?

More Related Hubs

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests