Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between width, naturalWidth, and clientWidth?

Tags:

dom

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

like image 398
galeb Avatar asked Jan 27 '15 09:01

galeb


People also ask

What is the difference between offsetWidth and clientWidth?

clientWidth is the inner width (ie. the space inside an element including padding but excluding borders and scrollbars) offsetWidth is the outer width (ie. the space occupied by the element, including padding and borders)

What is naturalWidth?

The naturalWidth property returns the original width of an image. For example, if you have an image that is originally 100 pixels wide. Then, you style the image with CSS/or by using the HTML "width" attribute to make it 500 pixels wide.

Does clientWidth include padding?

The Element. clientWidth property is zero for inline elements and elements with no CSS; otherwise, it's the inner width of an element in pixels. It includes padding but excludes borders, margins, and vertical scrollbars (if present).

What is offset width?

Typically, offsetWidth is a measurement in pixels of the element's CSS width, including any borders, padding, and vertical scrollbars (if rendered). It does not include the width of pseudo-elements such as ::before or ::after . If the element is hidden (for example, by setting style.


2 Answers

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.

like image 118
Vijay Avatar answered Oct 28 '22 16:10

Vijay


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.

like image 38
Chris Troutner Avatar answered Oct 28 '22 15:10

Chris Troutner