HTML DOM offsetWidth property
Example
Get the width of the div element, including padding and border:
var
elmnt
=
document
.
getElementById
(
"
myDIV
"
)
;
var
txt
=
"
The height of the div element, including padding and border:
"
elmnt
.
offsetHeight
"
px
"
;
txt
+=
"
The width of the div element, including padding and border:
"
elmnt
.
offsetWidth
"
px
"
;
Try it »
Definition and Usage
The offsetWidth property is a read-only property that returns the pixel width of the element, including padding and border but not margin; it is an integer measured in pixels (px).
Usually, the offsetWidth of an element is a measure of its CSS width, including the element's border, padding and horizontal scrollbar (if present and rendered), but excluding the width of pseudo-class elements such as :before or :after.
For the document's body object, it includes the total CSS width of the replaced elements linearly. The downward-extending content width of floating elements is ignored.

If the element is hidden (for example: the element or one of its ancestor elements has its style.display set to none), it returns 0.
Note: To understand this property, you can refer to CSS Box Model.
Tip: This property is usually used together with the offsetHeight property.
Tip: Use clientHeight and clientWidth The property returns the visible height and width of the element, including only padding.
Tip: To add a scrollbar to an element, you can use overflow attribute.
This is a read-only property.

Browser support
| property | |||||
|---|---|---|---|---|---|
| offsetWidth | Yes | Yes | Yes | Yes | Yes |
Syntax
element. offsetWidth
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
+=
"
The height includes padding:
"
elmnt
.
clientHeight
"
px
"
;
txt
+=
"
The height includes padding and border:
"
elmnt
.
offsetHeight
"
px
"
;
txt
+=
"
The width includes padding:
"
elmnt
.
clientWidth
"
px
"
;
txt
+=
"
The width includes padding and border:
"
elmnt
.
offsetWidth
"
px
"
;
Try it »
Example
The following example demonstrates the difference between the clientHeight/clientWidth and offsetHeight/offsetWidth properties after a scrollbar is added to the element:
var
elmnt
=
document
.
getElementById
(
"
myDIV
"
)
;
var
txt
=
"
"
;
txt
+=
"
The style information of the div:
"
;
txt
+=
"
The height includes padding:
"
elmnt
.
clientHeight
"
px
"
;
txt
+=
"
The height includes padding, border and scrollbar:
"
elmnt
.
offsetHeight
"
px
"
;
txt
+=
"
The width includes padding:
"
elmnt
.
clientWidth
"
px
"
;
txt
+=
"
The width includes padding, border and scrollbar:
"
elmnt
.
offsetWidth
"
px
"
;
Try it »
Element object