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.
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.
1
2
3
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;}
Well handy - thank you! Works without problems that I can find...
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?
I find this helpful.
Thanks, at last I've found a simple solution to my problem. Without having to read a 6 page article!
I've tried about 6 solutions and this is the only one that works! It's also XHTML complaint! Thanks!
Great article! I love beer!
Perfect =D thanks
I LOVE YOU!
jk Thanks
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.
thanks but what happens if JS is disabled on your user's site? back to square 1 eh..
i love you men thanks
Many thankx man.
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! :-)
Legend man!
I love you. You nailed it!
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?
so awesome, works...
thanks so much - its just cool
Awesome! Thanks for the snippet
this is so cool,
exactly what i was looking for...
Works great for me! Tks for sharing.
Thanks a lot - very useful! I used it on 2 field sets and it worked brilliantly
Not working in Windows Safari. Columns do not expand to equal height. Also IE8's debug console for js is showing an error on the 2-column function, for the rh variable "object expected". It is fine with the lh variable, but is giving an object required error on rh.
thx for the tutorial - didnt know about offsetheight
Works with me in Safari (on windows vista) The only thing is that they dont resize anymore when the browserwindow is made very small. Is it possible to implement that? So the content never flows out of the div. Cause my browsers now just give a large bottom margin. Like Nice, but.... says.
Is there a solution?
Thanks....The information have you published is really good....
Thanks Once Again...
Thanks man! Great work - you made my day!
Its really great man you have made it...
Cheers!
Thank you for this handy workaround for equal height div. I was always using Faux column method but it has some limitations. By using this method it came very handy to add different bg images for both the columns.
Joomla Templates
I've used this function on a couple sites I've done and I'm just encountering a weird glitch with it. Specifically I've expanded it to equal three columns instead of two, applied to three divs in the middle area of the page,and it worked just fine. Until, that is, I lookedin IE. In IE it resizes a fourth, completely seperate div. the IDs are in no way similar. I know it'sthe javascript becuase removing it causes the 4th div to remain it's original size.
Daniel,
That's interesting. Can you show me the page?
You can also do this with JQuery
function fixHeight(jQuerySelector) {
var heighest = -1, items = $(jQuerySelector);
if (items.length > 0) {
items.each(function (index) {
$(this).css("height", "auto");
heighest = Math.max(heighest, $(this).height());
});
items.each(function (index) {
$(this).height(heighest);
});
}
}
And they say divs are better than tables....
This is why google still use tables everywhere
BTW you can do this purely in CSS. It just takes alot of div's, see: http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks
personal narrative essay examples <a href="https://essaymeshing.com/ ">no essay scholarship</a> reflection essay example
synonym for essay <a href="https://essaymesh.com/ ">narrative essay example</a> read my essay
what is furosemide tablets used for <a href="https://lasixona.com/ ">furosemide (lasix</a> is furosemide an antibiotic
Where was this page a couple months ago when I needed it!!
Nice tutorials & examples BonRouge, you get a spot in my bookmarks.