HTML DOM offsetHeight Property

元素对象参考手册Element Object

Example

Get the height of a 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<br>"; txt += "The width of the div element, including padding and border:" + elmnt.offsetWidth + "px";

Try it Yourself »

Definition and Usage

The offsetHeight property is a read-only property that returns the pixel height of the element, including padding and border, excluding margin, as an integer in pixels px.

Generally, an element's offsetHeight is a measure of the element's CSS height, including the element's border, padding, and horizontal scrollbar (if present and rendered), but excluding:beforeor:afterthe height of pseudo-elements, etc.

For the document's body object, it includes the linear total content height instead of the element's CSS height. The height of content extending downward from floating elements is ignored.

If the element is hidden (for example, the element or one of its ancestor elements'style.displayis set tonone), returns 0.

Note:To understand this property, refer toCSS Box Model。

Tip:This property is usually used together with theoffsetWidthproperty.

Tip:Use the clientHeightandclientWidthproperty to return the visible height and width of the element, including only padding.

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

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 += "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 a scrollbar is added 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