jQuery height()Method

jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Examples

Return the height of the <div> element:

$("button").click(function(){ alert($("div").height()); });

Try it yourself »

Definition and Usage

The height() method sets or returns the height of the selected elements.

When this method is used toreturnthe height,it returns the height of the first matched element.

When this method is used tosetthe height, it sets the height of all matched elements.

As shown in the image below, this method does not include padding, border, or margin.

jQuery Dimensions


Related methods:

  • width()- Set or return the width of elements
  • innerWidth()- Return the width of elements (including padding)
  • innerHeight()- Return the height of elements (including padding)
  • outerWidth()- Return the width of elements (including padding and border)
  • outerHeight()- Return the height of elements (including padding and border)

Syntax

Return the height:

$(selector).height()

Set the height:

$(selector).height(value)

Set the height using a function:

$(selector).height(function(index,currentheight))

Parameter Description
value Required when setting the height. Specifies the height of the element, in units such as px, em, pt, etc.
The default unit is px.
function(index,currentheight) Optional. Specifies a function that returns the new height of the selected elements.
  • index- The index position of the element in the set.
  • currentheight- The current height of the selected element.


实例

More Examples

Set the height of an element
How to set the height of an element using different units.

Increase the height using a function
How to increase the height of an element using a function.

Return the height of the document and window elements
How to return the current height of the document and window elements.

Display dimensions using related methods
How to use width(), height(), innerHeight(), innerWidth(), outerWidth(), and outerHeight().


jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Other extensions