Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get div height in pixels although its height set to 100%

I wonder if there is any way to get div height in pixels, although its height set earlier to 100% height.
This is required as div content is dynamic so div height has different values based on content itself.

[Edit] Div by default is hidden.

I need to get div height in pixels for later manipulation (smooth scrolling will be done for div)?
Is there any way to do this?

like image 313
Ahmed Atia Avatar asked Jun 23 '10 11:06

Ahmed Atia


People also ask

How do I change my height to 100% in CSS?

height:100vh The . box class has only 100vh which is 100% of the viewport height. When you set the height to 100vh, the box element will stretch its height to the full height of the viewport regardless of its parent height.

How do you find the height of an element in pixels?

To get the height and width of an HTML element, you can use the offsetHeight and offsetWidth properties. These properties return the viewable height and width of an element in pixels, including border, padding, and scrollbar, but not the margin.

How is div height calculated?

You can use 2 properties, clientHeight and offsetHeight to get the height of the div. clientHeight includes padding of the div. offsetHeight includes padding, scrollBar, and borders of the div.


4 Answers

theDiv.clientHeight
like image 171
Deniz Dogan Avatar answered Oct 25 '22 16:10

Deniz Dogan


Since you tagged jQuery, use

$("#myElement").height();

http://api.jquery.com/height/

For Plain Ol' Javascript, you can use element.clientHeight or element.offsetHeight, depending on which one suits your needs.


Since the div is hidden, you will have to .show() it before calling .height(), and you can hide it again straight away:
var $myEl  = $('#myElement').show();
var height = $myEl.height();
$myEl.hide();
like image 41
Andy E Avatar answered Oct 25 '22 18:10

Andy E


You can use height()

$("#divInQuestion").height();
like image 40
Russell Dias Avatar answered Oct 25 '22 17:10

Russell Dias


You could use the .height() function:

$('#divid').height()
like image 29
Darin Dimitrov Avatar answered Oct 25 '22 17:10

Darin Dimitrov