Get height of a div element with JavaScript/jQuery
This post will discuss how to get the height of a div element in JavaScript and jQuery.
1. Using JavaScript
In JavaScript, you can use the clientHeight property, which returns an element’s height, including its vertical padding. Basically, it returns the actual space used by the displayed content.
For example, the following code returns 120, which is equal to the original height plus the vertical padding.
JS
|
1 2 |
var clientHeight = document.getElementById('container').clientHeight; alert(clientHeight); |
HTML
|
1 |
<div id="container" class="some-class"></div> |
CSS
|
1 2 3 4 5 6 |
.some-class { border: 1px solid; width: 500px; height: 100px; padding: 10px; } |
Alternatively, if you need border and scrollbars as well, consider using the offsetHeight property. It returns the height of an element, including vertical padding and borders. In other words, it returns the total amount of space an element occupies.
For example, the following code will return 122 value which is equal to the original height plus the vertical padding plus the border.
JS
|
1 2 |
var offsetHeight = document.getElementById('container').offsetHeight; alert(offsetHeight); |
HTML
|
1 |
<div id="container" class="some-class"></div> |
CSS
|
1 2 3 4 5 6 |
.some-class { border: 1px solid; width: 500px; height: 100px; padding: 10px; } |
You can also use the height property to get the height of an element. This only works when the container has a height attribute set. Note, this returns a value with units intact.
JS
|
1 2 |
var height = document.getElementById('container').style.height; alert(height); |
HTML
|
1 |
<div id="container" style="border: 1px solid; height: 100px; width: 500px;"></div> |
Finally, you can also use the getBoundingClientRect() method. It returns a DOMRect object which has the height property, whose value includes the padding and border.
For example, the following code will return 122 value which is equal to the original height plus the vertical padding plus the border.
JS
|
1 2 3 |
var rect = document.getElementById('container').getBoundingClientRect(); var height = rect.height; alert(height); |
HTML
|
1 |
<div id="container" class="some-class"></div> |
CSS
|
1 2 3 4 5 6 |
.some-class { border: 1px solid; width: 500px; height: 100px; padding: 10px; } |
2. Using jQuery
jQuery has the .height() method, which returns an element’s content height. For example, the following code returns 100, which is equal to the original height of div.
JS
|
1 2 3 4 |
$(document).ready(function() { var height = $("#container").height(); alert(height); }); |
HTML
|
1 |
<div id="container" class="some-class"></div> |
CSS
|
1 2 3 4 5 6 |
.some-class { border: 1px solid; width: 500px; height: 100px; padding: 10px; } |
Note, the .height() method returns a unit-less value. If you need height with units intact like 100px, use .css("height") instead.
JS
|
1 2 3 4 |
$(document).ready(function() { var height = $("#container").css("height"); alert(height); }); |
HTML
|
1 |
<div id="container" class="some-class"></div> |
CSS
|
1 2 3 4 5 6 |
.some-class { border: 1px solid; width: 500px; height: 100px; padding: 10px; } |
Alternatively, if you need to include padding and border information, and optionally the margin, consider using the .outerHeight() method.
JS
|
1 2 3 4 |
$(document).ready(function() { var height = $("#container").outerHeight(); alert(height); }); |
HTML
|
1 |
<div id="container" class="main-class"></div> |
CSS
|
1 2 3 4 5 6 |
.main-class { border: 1px solid gray; width: 500px; height: 100px; padding: 10px; } |
That’s all about getting the height of a div element in JavaScript and jQuery.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)