clientWidth property
The clientWidth property contains
the width of an element inside borders
along with
padding
but without
border
and scroll.
Syntax
javascriptelement.clientWidth;
Example
Let's find the size of an element:
html<div id="elem"></div>css#elem {
width: 100px;
height: 100px;
border: 10px solid black;
padding: 15px;
}javascriptlet elem = document.querySelector('#elem');
console.log(elem.clientWidth);
The code execution result:
javascript130
Example
If an element has a scroll, then the
width of a content is reduced by the width
of a scroll (about 16px - depends
on the browser, OS, device). In the
following example, the width of the
element will be smaller than expected:
html<div id="elem">This element has a scroll.</div>css#elem {
width: 100px;
height: 100px;
border: 10px solid black;
padding: 15px;
overflow: scroll;
}javascriptlet elem = document.querySelector('#elem');
console.log(elem.clientWidth);
The code execution result:
javascript114 (depends on the browser)
Example
If an element is hidden, then
clientWidth will be equal
to 0:
html<div id="elem"></div>css#elem {
display: none; /* hidden element */
width: 100px;
height: 100px;
border: 10px solid black;
padding: 15px;
}javascriptlet elem = document.querySelector('#elem');
console.log(elem.clientWidth);
The code execution result:
javascript0
See also
-
the
clientHeightproperty
that contains the height of an element inside borders -
the
getComputedStylemethod
which obtains the value of an element's CSS property