The height method
The height method allows you to get and change the height of an element. It is important to remember that we will get 'calculated value'
height. Unlike the css('height') method, it returns a unitless value (for example 400) and is convenient for mathematical calculations. The method gets the height of the element's content, regardless of the CSS property box-sizing specified. To avoid unnecessary calculations, it is recommended to use css('height'). Calculation errors can also occur if the user resizes the page, or if the element or its parent is hidden. The height value does not take into account padding and border values.
Syntax
Get the height of an element. In some cases, the resulting values may be fractional:
$(selector).height();
To change the height of an element, you can simply pass a number (for example, 400), then the units of measurement will be pixels, or a string indicating the units of measurement (for example, '10em'):
$(selector).height(new value);
We can also apply a given function to each element in the set. In this case, the function will receive the number of the element in the set as the first parameter, and the current value of the given height for a specific element as the second parameter. this inside the function will point to the current element.
The value of the element's height will change to the one returned by the function:
$(selector).height(function(number in set, current height value));
Example
Let's change its height to 30px when clicked, using the height method, and also change its background to green using css:
<div id="test"></div>
#test {
width: 50px;
height: 90px;
background: red;
color: white;
margin-top: 10px;
cursor: pointer;
}
$('#test').one('click', function() {
$(this).height(30).css({
cursor: 'auto',
backgroundColor: 'green'
});
});
See also
-
method
width,
which allows you to get and change the width of an element -
method
innerHeight,
which allows you to get and change the height of an element, taking into account its internal paddings -
method
outerHeight,
which allows you to get and change the height of an element, taking into account its padding and border -
method
css,
which allows you to get and change the CSS styles of an element