Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM offsetHeight property

HTML DOM offsetHeight property

Element Object Reference Element object

Example

Get the height 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 offsetHeight property is a read-only property that returns the pixel height of the element, including padding and border but not margin; it is an integer measured in pixels (px).

Usually, the offsetHeight of an element is a measure of its CSS height, including the element's border, padding and horizontal scrollbar (if present and rendered), but excluding the height of pseudo-class elements such as :before or :after.

For the document's body object, it includes the total CSS height of the replaced elements linearly. The downward-extending content height 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 offsetWidth 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
offsetHeight Yes Yes Yes Yes Yes

Syntax

element.offsetHeight

Technical Details

Return Value: Returns an integer representing the pixel height 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 Reference Element object