Equal height - javascript

Many people have problems getting the heights of their divs the same length. There are good, valid, ways of doing this with only CSS. This isn't one of them. This uses javascript to find out which div is the longest and then set the others to be the same length as that one.

fixH( )

1

2

3

4

5

6

7

8

9

10

11

This div has 12 paragraphs

1

2

3

This div has 4 paragraphs but it's as long as the other one because of some javascript which makes the shorter div the same length as the longer one.

fixH2( )

1

2

3

4

5

6

7

1

2

3

4

5

javascript
function fixH(one,two) {
if (document.getElementById(one)) {
var lh=document.getElementById(one).offsetHeight;
var rh=document.getElementById(two).offsetHeight;
var nh = Math.max(lh, rh);
document.getElementById(one).style.height=nh+"px";
document.getElementById(two).style.height=nh+"px";
}
}

//this does it for three
function sortNum(a,b) { return b-a}
function fixH2(one,two,three) {
if (document.getElementById(one)) {
var obj=new Array(3);
var option=[one,two,three];
for(var i=0; i<option.length; i++) {
document.getElementById(option[i]).style.height="auto";
obj[i]=document.getElementById(option[i]).offsetHeight;
nh=obj.sort(sortNum);
}
nh1=nh.splice(1,2);
for(var i=0; i<option.length; i++) {
document.getElementById(option[i]).style.height=nh+"px";
}
}
}
//


window.onload=function(){
fixH('leftcolumn','rightcolumn');
fixH2('left','right','centre');
}
CSS
#leftcolumn {
width:300px;
background: red;
text-align:center;
float:left;
}
#rightcolumn {
width:300px;
background: blue;
color:white;
float:right;
text-align:center;
}
#left, #right, #centre {
width:200px;
text-align:center;
}
#left {background:red; float:left;}
#centre {
background:white;
border:1px solid gray;
margin:0 auto;
}
#right {background:blue; color:white; float:right;}

Comments

#1
2006-06-16 JazzLad says :

Where was this page a couple months ago when I needed it!! smile

Nice tutorials & examples BonRouge, you get a spot in my bookmarks.

#2
2006-08-30 Gelatinous_Blob says :

Well handy - thank you! Works without problems that I can find...

#3
2006-10-05 MSpann says :

Thanks for this!
Now, I'm not a JavaScript programmer, so this may be a dumb question, but can this script be modified to:
1) Ensure that all columns are not only equal length, but also reach the bottom of the screen (if the content doesn't reach), and
2) If another script changes the length of one column (ie: an expanding menu), can this script be re-triggered to compensate for the length change?

#4
2006-11-16 Peeyush says :

I find this helpful.

#5
2007-01-15 paul says :

Thanks, at last I've found a simple solution to my problem. Without having to read a 6 page article!

#6
2007-02-25 Tris says :

I've tried about 6 solutions and this is the only one that works! It's also XHTML complaint! Thanks!

#7
2007-05-03 BlackNine says :

Great article! I love beer!

#8
2007-07-11 Me says :

Perfect =D thanks

#9
2007-09-12 twodayslate says :

I LOVE YOU!

jk Thanks

#10
2008-01-26 Jacek says :

Hi,
very nice solution. It breaks if user uses text resize, because both columns will keep their old fixed size no matter what happens with the text inside. In such case sth. like http://www.hedgerwow.com/360/dhtml/js-onfontresize.html
might help.

#11
2008-02-01 shez says :

thanks but what happens if JS is disabled on your user's site? back to square 1 eh..

#12
2008-02-20 john says :

i love you men thanks

#13
2008-03-19 kurinchilion says :

Many thankx man.

#14
2008-03-25 Albert Vila says :

Oh!! Bowing on you! :-)
I had the problem of the non-matching heights for weeks, always pushing it aside at every frustrating failed attempt to fix it... til the end of the website, when I couldn't wait anymore and I thought about JS instead of the damn CSS. Searching I came to your website to finally solving it!

Thanks SO much! :-)

#15
2008-04-09 per says :

Legend man!

#16
2008-06-30 a fan says :

I love you. You nailed it!

#17
2008-09-05 nice, but... says :

I've racked my brain trying to get height:100%; work in css. I couldn't get it to work.

This solution looks great but it fails when the width is a percent. The height of the div fails to expand when the width of the browser is reduced.

Does anyone have a solution for this?

#18
2008-10-15 simpanc says :

so awesome, works...
thanks so much - its just cool very happy

#19
2009-01-31 Bala says :

Awesome! Thanks for the snippet

#20
2009-04-29 sergiugothic says :

this is so cool,
exactly what i was looking for...

#21
2009-05-14 Quinn says :

Works great for me! Tks for sharing.

Comment form

Please type the word 'whisky' here:

BB code available :

  • [b]...[/b] : bold
  • [it]...[/it] : italic
  • [q]...[/q] : quote
  • [c]...[/c] : code
  • [url=...]...[/url] : url