English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
clientWidthA read-only property that returns the visible width of an element (in pixels), including padding but not including border, margin, and vertical scrollbar (if it exists).
The calculation formula for clientWidth is as follows: CSS width + CSS padding-the width of the vertical scrollbar (if it exists).
To understand this property, you must understandCSS Box Model.
Usage:offsetWidthThis property can return the visible width of an element, including padding, border, and vertical scrollbar.
element.clientWidth
var elem = document.querySelector("div"); var txt = "Height includes padding: " + elem.clientHeight + "px<br>"; txt += "Width includes padding: " + elem.clientWidth + "px";Test and See‹/›
All browsers fully support the clientWidth property:
Property | |||||
clientWidth | Yes | Yes | Yes | Yes | Yes |
Return Value: | A number representing the width of the element (in pixels), including padding |
---|
This example demonstrates the difference between clientWidth and offsetWidth:
var elem = document.querySelector("div"); var txt = "Width includes padding: " + elem.clientWidth + "px<br>"; txt += "Width includes padding"+Border: " + elem.offsetWidth + "px";Test and See‹/›
HTML DOM Reference:HTML DOM offsetWidth Property
HTML DOM Reference:HTML DOM scrollWidth Property
CSS Reference:CSS overflow Property