Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out divs height and setting div height

I'm quite new to javascript/jquery stuff, but i would like to do this kind of thing with it:

I have four divs side-by-side like this and content in each one. Now if one has more content it gets stretched higher. I would like to make the other divs as high too even if they don't have as much content. So basically i want the script goes through the divs and check the heights and sets all of divs heights to same as the highest div has. Hopefully you understand :)

like image 422
mkoso Avatar asked Feb 22 '10 18:02

mkoso


People also ask

How do I know my div height?

You can use the clientHeight property to measure the inner height of an element, including padding. However, it will exclude the borders, margins and scrollbar height of the element. Now coming to the client height, the internal content along with border is considered as the client height.

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.


2 Answers

I was getting a NaN error with SLaks code. Giving maxHeight an initial value of 0 did the trick.

var maxHeight = 0;
$('div')
  .each(function() { maxHeight = Math.max(maxHeight, $(this).height()); })
  .height(maxHeight);

Thanks SLaks!

like image 78
Fireflight Avatar answered Sep 25 '22 17:09

Fireflight


You can use jQuery's height method to get and set the height of an element.

You need to loop through the elements, find the tallest one, then set the height of all of the elements.

var maxHeight;
$('div')
    .each(function() { maxHeight = Math.max(maxHeight, $(this).height()); })
    .height(maxHeigt);

Replace 'div' with a jQuery selector that selects the elements you want to equalize.

like image 41
SLaks Avatar answered Sep 26 '22 17:09

SLaks