11

What is the difference between width, naturalWidth, and clientWidth?

1
  • This has nothing to do with JavaScript. It's a question about DOM properties. Commented Jan 27, 2015 at 9:36

2 Answers 2

10

Read this:

Understanding offsetWidth, clientWidth, scrollWidth and -Height, respectively

example: <img> tag

naturalWidth: it is the original width of the image used in tag.

width: it is the value/default value of width attribute of tag.

Sign up to request clarification or add additional context in comments.

3 Comments

is there any way to get original width of image before loading the image?
use Load event of image and get the width & height or you can use: var w = document.querySelector('img').naturalWidth;
You didnt explain clientWidth
3

clientWidth represents the width of the element and is subject to manipulation by the browser. For example, a 300px wide image can be resized and this manipulated value will come through in clientWidth.

naturalWidth is the natural width of the element. In the example of a 300px wide image, the naturalWidth will still be 300 even if the image is resized in the browser via CSS or JavaScript.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.