Get the current computed height for the first element in the set of matched elements or set the height of every matched element.

.height()Возвращает: Number

Описание: Get the current computed height for the first element in the set of matched elements.

  • Добавлен в версии: 1.0.height()

    • This method does not accept any arguments.

The difference between .css( "height" ) and .height() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). The .height() method is recommended when an element's height needs to be used in a mathematical calculation.

Figure 1 - Illustration of the measured height

This method is also able to find the height of the window and document.

// Returns height of browser viewport
$( window ).height();
// Returns height of HTML document
$( document ).height();

Note that .height() will always return the content height, regardless of the value of the CSS box-sizing property. As of jQuery 1.8, this may require retrieving the CSS height plus box-sizing property and then subtracting any potential border and padding on each element when the element has box-sizing: border-box. To avoid this penalty, use .css( "height" ) rather than .height().

Note: Although style and script tags will report a value for .width() or height() when absolutely positioned and given display:block, it is strongly discouraged to call those methods on these tags. In addition to being a bad practice, the results may also prove unreliable.

Дополнительные замечания:

  • The number returned by dimensions-related APIs, including .height(), may be fractional in some cases. Code should not assume it is an integer. Also, dimensions may be incorrect when the page is zoomed by the user; browsers do not expose an API to detect this condition.
  • The value reported by .height() is not guaranteed to be accurate when the element or its parent is hidden. To get an accurate value, ensure the element is visible before using .height(). jQuery will attempt to temporarily show and then re-hide an element in order to measure its dimensions, but this is unreliable and (even when accurate) can significantly impact page performance. This show-and-rehide measurement feature may be removed in a future version of jQuery.

Примеры использования

.height( value )Возвращает: jQuery

Описание: Set the CSS height of every matched element.

  • Добавлен в версии: 1.0.height( value )

    • value
      Тип: String or Number
      An integer representing the number of pixels, or an integer with an optional unit of measure appended (as a string).
  • Добавлен в версии: 1.4.1.height( function )

    • function
      Тип: Function( Integer index, Integer height ) => String or Number
      A function returning the height to set. Receives the index position of the element in the set and the old height as arguments. Within the function, this refers to the current element in the set.

When calling .height(value), the value can be either a string (number and unit) or a number. If only a number is provided for the value, jQuery assumes a pixel unit. If a string is provided, however, a valid CSS measurement must be provided for the height (such as 100px, 50%, or auto). Note that in modern browsers, the CSS height property does not include padding, border, or margin.

If no explicit unit was specified (like 'em' or '%') then "px" is concatenated to the value.

Note that .height(value) sets the content height of the box regardless of the value of the CSS box-sizing property.

Примеры использования