I have used jQuery library to find out height of a div
.
Below is my div
element with attributes :
<DIV id="myDiv" style="height:auto; width:78;overflow:hidden"> Simple Test</DIV>
Below is my jQuery code to get height of <div>
var result = $("#myDiv").css('height'); alert(result);
After executing above statement I am getting result as "auto". Actually this is I am not expecting, instead of that I want the result in px dimension.
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.
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.
All block level elements inherit the width of their parent element by default. In your example, the div is inheriting the width of the parent body tag, taking into account any margin or padding on the body element. this doesn't mention height. Which is max(0,content).
Use .height()
like this:
var result = $("#myDiv").height();
There's also .innerHeight()
and .outerHeight()
depending on exactly what you want.
You can test it here, play with the padding/margins/content to see how it changes around.
Although they vary slightly as to how they retrieve a height value, i.e some would calculate the whole element including padding, margin, scrollbar, etc and others would just calculate the element in its raw form.
You can try these ones:
javascript:
var myDiv = document.getElementById("myDiv"); myDiv.clientHeight; myDiv.scrollHeight; myDiv.offsetHeight;
or in jquery:
$("#myDiv").height(); $("#myDiv").innerHeight(); $("#myDiv").outerHeight();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With