Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate the height of a div with some text inside and a fixed width

I have the following code:

<div class="content">
                        <a href="/profile/Nat's Rare &amp; 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.

like image 683
aherlambang Avatar asked Jun 07 '13 04:06

aherlambang


People also ask

How do you find the height of a div based on its content?

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.

How do you find the inner content height?

var height = document. getElementById('content'). clientHeight; That will not include any borders or scrollbars in the element, just padding.

How can we get dynamic height of div?

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.

Can we calculate height of div in CSS?

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.


1 Answers

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

  1. Append the element to the body with respective innerHTML's
  2. Find the height using any of the above methods
  3. Now,remove the element and of course , you found the height & element is not there

See Fiddle :To get an element's rendered height

like image 90
Prasath K Avatar answered Oct 14 '22 00:10

Prasath K