I have the following code:
<div class="content">
<a href="/profile/Nat's Rare & Raw" class="link-name"><strong>Irene Natalia</strong></a>
Hooray! we'll proceed with the shipping and inform the tracking # tomorrow :) Thank you, Angel! :)
<br>
<span class="date">7 days ago</span>
</div>
I wanted to know the height of this div from using javascript. Is there a way to do so? This div has a fixed sized width (in this case 150px). I just wanted to know the height of the div say the text changed. Preferred method is via jQuery but javascript is fine as well
EDIT:
A very important note here is that this is a calculation before the div is being rendered. I am basically creating a pinterest layout and I need to calculate the height of the comments in the item card so that masonry can lay it out first before the image is loaded.
Method 1: Using the offsetHeight property: The offsetHeight property of an element is a read-only property and used to return the height of an element in pixels. It includes any borders or padding of the element. This property can be used to find the height of the <div> element.
var height = document. getElementById('content'). clientHeight; That will not include any borders or scrollbars in the element, just padding.
We use css property height: calc( 100% - div_height ); Here, Calc is a function. It uses mathematical expression by this property we can set the height content div area dynamically.
Unfortunately, it is not possible to "get" the height of an element via CSS because CSS is not a language that returns any sort of data other than rules for the browser to adjust its styling. Your resolution can be achieved with jQuery, or alternatively, you can fake it with CSS3's transform:translateY(); rule.
Javascript:
element.offsetHeight;
This gives the height of an element
See about offsetHeight here and this is pure javascript.
jQuery:
$elem.height();
- gives height of the element without padding , border and margin
$elem.innerHeight();
- gives height including padding
$elem.outerHeight(true);
- gives height including padding, border and margin
EDIT:
If you want to find the height of the element before it is rendered , then i can give you one of many possible solutions
See Fiddle :To get an element's rendered height
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