css
CSS Equal Height Columns
On 20, Nov 2009 | 23 Comments | In css | By abcoder
Once upon a time people used tables for designing website layout. Definitely table tag has many advantages but it’s not that flexible & extensile. You can not use proper positioning or floating attributes css style for table based layout. It’s not that commodious to handle table width/height from Dreamweaver, the layout gets messy when you drag any border of table, td or tr as the dimensions automatically get changed. The most important incentive for the current web sphere is SEO (Search Engine Optimization) which is dramatically & automatically mended when DIV based layout is used where TABLE based layout is toilsome for search bots to crawl & takes much page loading time. And at some point it makes more sense to use table for displaying tabular formatted data only, for example gmail uses table for the email list, this could be done using UL/LI, but table is more appropriate in this case.
Lately most web designers use DIV for page layout designing for its flexibility and robustness. Unlike table div has a big problem (in fact it’s the nature of div) when you want to give same height to multi-columns div based page layout, you can not do that very easily. I always used javascript for this purpose, but wished if I could do it with css, cos when the javascript is disabled the page layout will be not as expected in most of the cases.
Yes it is possible to create equal height columns with css! You can make the height of multiple floated divs equal using CSS only and no javascript or invalid css. Before beginning I assume you have adequate knowledge on HTML, CSS, javascript and you have already Firebug add-on installed on your Firefox
Lemme start from the beginning:
Default three column layout
When you apply float left and fixed width to multiple adjacent divs, by default the layout looks like the above screen shot. You can see it from browser or have a look at the source code (html/css) on the following link above. You see as the contents are not same in the 3 divs, the height is different.
The next example is making the height of the 3 divs same using javascript (jQuery).
Three column layout – equal height using jQuery
This is mostly and widely used for making equal height columns using javascript. I’ve used jQuery here. You may have a look at the source code from the link above.
$(function(){
var H = 0;
$("div").each(function(i){
var h = $("div").eq(i).height();
if(h > H) H = h;
});
$("div").height(H);
});
Now here comes the css equal height technique! The mantra is to use a large amount of bottom padding and the same amount of negative bottom margin to the floated divs and keep them inside an overflow hidden div. That’s all, and the magic happens!
Three column layout – equal height using pure CSS
Let me get into details on how to do it. At First have a look at this link. Here I’ve used a huge amount (9000px) of bottom padding and the same amount of negative bottom margin (-9000px). But this makes the columns look so long and still not equal height at bottom. Now on this link I’ve used a holder div with overflow:hidden and that’s it, it is doing the magic by hiding the overflow section.
body{margin-bottom:50px}
div.holder { overflow:hidden }
div.holder div { float:left; width:30%; background-color:#9C0; margin-right:5px; padding:10px; padding-bottom:9000px; margin-bottom:-9000px }
Hope it’s clear now.
There is another method which use a background image with 1px height and color matched with the div’s bg color and body’s bg color. Please have a look at the html link, it’s self-explanatory.
Three column layout – equal height using pure CSS with Background Image
Now I’ll try border to the columns and we’ll see the problem and it’s solution.
Three column layout – equal height with border
Here you can see bottom border is missing which is obvious as we’ve used a huge bottom padding, from Firebug if you turn off overflow:hidden to the holder div and scroll down at the bottom of the page you’ll see the bottom border there but not equal height.
So how to solve this issue? There are 2 ways to do it – using a bg image in a tricky way or another pure tricky css hack.
Three column layout – equal height with bottom border fix using bg image
Here I’ve used a 1px bg image for the background image and placed it at left-bottom of the bottom-border div with no-repeat and also applied 1px bottom padding. The holder div is inside the bottom-border div. Please feel free to play around with firebug!
This is the last method for bottom border fix without any bg image, just pure css.
Three column layout – equal height with bottom border fix using pure css and no bg image
I’m not gonna explain the details of this last method, find it out yourself!
Get professional testking 1z0-007 web designing training to learn how to create different web site layouts with css format. Become expert using testking 642-415 design tutorials and testking 642-631 css live demos.
-
Nice pure-css solution at the bottom. It falls apart a little in IE6, but, most elegant solutions do.
-
thank you @ejohnson
-
Thank you for the solution of pure-css but it fails with opera browser when there is some div with position absolute in one of three divs.
have you a suggestion?
-
You can see the example of this issue on http://www.antoniooliva.net/new
-
Hi,
is there anyone that can answer me? -
You can see with opera the website that you can open with click on my name.
Else, where i’ll can send yu the screenshot?
Thanks
-
Hi Adam,
i sented you a screenshot yesterday evening. do you have a suggestion for my problem? -
wow… even on Opera 10.62 the page height is so long to below!
-
this was such a beneficial posting. I will be seriously awaiting the subsequent post.
-
Same result with minimal effort and better flexibility – http://www.webdevelopers.eu/jquery/fixheight/demo
-
The solution of abc coder is better then of yours. This isn’t the same result because you use javascript.
-
-
what happens if there div inside that holder div?
got a solution for that?
-
i think this works:
http://www.datatex.org/the-best-solution-for-css-columns-with-equal-height.html -
Fantastic codes…. Very useful for me.. thanks a lot for posting…
Cheers,
Mani -
jQuery plug-in works great, except in IE, even IE8 the columns are much too long. Is there an IE workaround?
-
Hey! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I’m getting fed up of WordPress because I’ve had problems with hackers and I’m looking at options for another platform. I would be fantastic if you could point me in the direction of a good platform.
-
you can use joomla
-
Next time – View Source. Lots of information there.
-
-
THANK YOU FOR THIS BEE-UTIFUL WEBPAGE!!!
I admire the methodologies and the simple layouts and source code, letting me choose my plan of attack. I can now add another feather to my CSS cap!!
Kind regards, Mango.
-
Hi, you have a Nice section of content. I just stumbled upon your web site. The clarity in your post is just excellent. Anyway I’ll be subscribing to your post, if possible ! I suppose its ok to borrow a few of your great ideas! You really have a talent for writing. Thanks.
-
I want first and third tables of same height but the height of middle can be anything. Can you please suggest on this. Height of third table should become equal to height of first table .
-
Holla! is this looks ok in IE browsers ?
Submit a Comment
© Copyright 2012 ABCoder |




















Comments