Get div height without height set in css

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

+56
jquery css height
Mar 06 2018-12-12T00:
source share
4 answers

jQuery .height will return you the height of the element. It does not require a CSS definition because it determines the calculated height.

Demo

You can use .height() , .innerHeight() or outerHeight() based on what you need.

enter image description here

.height() - returns the height of the element, excluding indents, borders and fields.

.innerHeight() - returns the height of the element, including padding, but excludes border and margin.

.outerHeight() - returns the height of the div, including the border, but excludes the margin.

.outerHeight(true) - returns the height of the div, including the margin.

Check out the code snippet below for a 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> 
+150
Mar 06 '12 at 22:00
source share

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, the height of which is determined by its contents, you need to start with window.load or window.scroll not document.ready , otherwise I get odd heights / lower heights, i.e. before uploading images. I also used externalHeight () .

 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()); }); 
+32
Oct 25
source share

Also make sure the div is added to the DOM and visible .

+6
Jun 18 '14 at 2:36
source share

You can do this in jQuery . Try all .height() , .innerHeight() or .outerHeight() .

 $('document').ready(function() { $('#right_div').css({'height': $('#left_div').innerHeight()}); }); 

Screenshot example

enter image description here

Hope this helps. Thank!!

+4
Sep 06 '14 at 10:56 on
source share



All Articles