HTML DOM clientWidth Property

元素对象参考手册Element Object

Example

Get the width of a div element, including padding:

var elmnt = document.getElementById("myDIV"); var txt = "The height of a div element, including padding:" + elmnt.clientHeight + "px<br>"; txt += "The width of a div element, including padding:" + elmnt.clientWidth + "px";

Try it yourself »

Definition and Usage

The clientWidth property is a read-only property that returns the pixel width of the element. The width includes padding, but does not include border, margin, or scrollbar. It is an integer, in pixels (px).

For inline elements and elements without CSS styles, the clientWidth property value is 0.

Note:To understand this property, seeCSS Box Model。

Tip:This property is usually used with theclientHeightproperty together.

Tip:Use the offsetHeightandoffsettWidthproperty to return the visible height and width of an element, including padding and border.

Tip:To add a scrollbar to an element, you can use theoverflowproperty.

This is a read-only property.


Browser Support

Property
clientWidth Yes Yes Yes Yes Yes

Syntax

element.clientWidth 

Technical Details

Return Value: Returns an integer representing the pixel width of the element.

More Examples

Example

The following example demonstrates the difference between the clientHeight/clientWidth and offsetHeight/offsetWidth properties:

var elmnt = document.getElementById("myDIV"); var txt = ""; txt += "Height includes padding:" + elmnt.clientHeight + "px<br>"; txt += "Height includes padding and border:" + elmnt.offsetHeight + "px<br>"; txt += "Width includes padding:" + elmnt.clientWidth + "px<br>"; txt += "Width includes padding and border:" + elmnt.offsetWidth + "px";

Try it yourself »

Example

The following example demonstrates the difference between the clientHeight/clientWidth and offsetHeight/offsetWidth properties after adding a scrollbar to the element:

var elmnt = document.getElementById("myDIV"); var txt = ""; txt += "<b>The div's style information:</b><br>"; txt += "Height includes padding:" + elmnt.clientHeight + "px<br>"; txt += "Height includes padding, border, and scrollbar:" + elmnt.offsetHeight + "px<br>"; txt += "Width includes padding:" + elmnt.clientWidth + "px<br>"; txt += "Width includes padding, border, and scrollbar:" + elmnt.offsetWidth + "px";

Try it yourself »


元素对象参考手册Element Object

Other Extensions