Is there any way to get the height of an element if there is no CSS height rule set for the element I cannot use .height()
jQuery method because it need a CSS rule set first? Is there any other way to get the height?

- 8,578
- 10
- 57
- 77

- 2,360
- 6
- 29
- 39
-
2What makes you think `height()` needs to have a css rule? – James Montagne Mar 06 '12 at 21:59
-
`height()` will get the elements' computed height... – elclanrs Mar 06 '12 at 22:00
-
1sounds like you are trying to get height of something inside a hidden element? or element itself is hidden. No can do! – charlietfl Mar 06 '12 at 22:01
-
Include your code because there is no requirement for `height` to have css set. That link has nothing to do with jquery. – James Montagne Mar 06 '12 at 22:06
-
that "insight" link is 7 years old! – charlietfl Mar 06 '12 at 22:11
-
You are all right! the problem was that my code was not closed properly sorry!! – Samuel Lopez Mar 06 '12 at 22:13
4 Answers
jQuery .height
will return you the height of the element. It doesn't need CSS definition as it determines the computed height.
You can use .height()
, .innerHeight()
or outerHeight()
based on what you need.
.height()
- returns the height of element excludes padding, border and margin.
.innerHeight()
- returns the height of element includes padding but excludes border and margin.
.outerHeight()
- returns the height of the div including border but excludes margin.
.outerHeight(true)
- returns the height of the div including margin.
Check below code snippet for live demo. :)
$(function() {
var $heightTest = $('#heightTest');
$heightTest.html('Div style set as "height: 180px; padding: 10px; margin: 10px; border: 2px solid blue;"')
.append('<p>Height (.height() returns) : ' + $heightTest.height() + ' [Just Height]</p>')
.append('<p>Inner Height (.innerHeight() returns): ' + $heightTest.innerHeight() + ' [Height + Padding (without border)]</p>')
.append('<p>Outer Height (.outerHeight() returns): ' + $heightTest.outerHeight() + ' [Height + Padding + Border]</p>')
.append('<p>Outer Height (.outerHeight(true) returns): ' + $heightTest.outerHeight(true) + ' [Height + Padding + Border + Margin]</p>')
});
div { font-size: 0.9em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="heightTest" style="height: 150px; padding: 10px; margin: 10px; border: 2px solid blue; overflow: hidden; ">
</div>

- 79,297
- 15
- 120
- 134
-
hello all is there a solution for this without jquery, using vanilla js? Have the same problem with elements without a specific height set. Every height calculation function here returns only zero for me. – Alex Aug 11 '22 at 09:52
Just a note in case others have the same problem.
I had the same problem and found a different answer. I found that getting the height of a div that's height is determined by its contents needs to be initiated on window.load, or window.scroll not document.ready otherwise i get odd heights/smaller heights, i.e before the images have loaded. I also used outerHeight().
var currentHeight = 0;
$(window).load(function() {
//get the natural page height -set it in variable above.
currentHeight = $('#js_content_container').outerHeight();
console.log("set current height on load = " + currentHeight)
console.log("content height function (should be 374) = " + contentHeight());
});

- 33,193
- 69
- 233
- 372

- 1,359
- 2
- 17
- 32
-
1Probably you're getting odd heights because there are more instructions that change the height after you have used/printed them. It's recommended to ask for heights at the end of your script – Gjaa Feb 17 '15 at 00:30
Can do this in jQuery. Try all options .height()
, .innerHeight()
or .outerHeight()
.
$('document').ready(function() {
$('#right_div').css({'height': $('#left_div').innerHeight()});
});
Example Screenshot
Hope this helps. Thanks!!

- 25,047
- 11
- 113
- 117
Also make sure the div is currently appended to the DOM and visible.

- 36,135
- 10
- 122
- 176

- 99
- 1
- 1
-
13Please note that this might me more suited as a comment than an answer. – kkuilla Jun 18 '14 at 14:54
-
5
-
4I consider this a crucial bit of information, because none of the other answers will work if the element is not attached to the DOM to begin with. – Guido Apr 29 '16 at 23:48