Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Equal column heights with CSS and JS 1

Status
Not open for further replies.

maxformed

Technical User
Apr 22, 2003
168
US
Picked this up from the sitepoint.com techtimes newsletter this morning. It's a good workaround.

" Quick Tip
Equalizing Column Heights

As I discussed in this issue's Editorial, creating columns of equal height is beyond the abilities of current CSS layout standards. Fortunately, a little JavaScript comes to the rescue in this Quick Tip.

So following the examples in the Editorial, we'll assume you have a page with three columns. The center column uses natural (i.e. static) positioning and includes margins that leave room for the left and right columns, which use absolute positioning. The id attributes of the column <div> tags are left, content, and right.

Instead of dealing with the differences between browsers, we'll leave it to the professionals and use the excellent X script from Cross-Browser.com. Simply download x.js from that site and load it in the <head> tag of your page as follows:

<script src=&quot;x.js&quot; type=&quot;text/javascript&quot;>
</script>

Now, because the footer may well be covered by the left and right columns when the browser lays them out, we'll want to keep the footer invisible until we've adjusted the column heights. Make sure the footer <div> has id=&quot;footer&quot; set and add this style rule to the <head> tag of your document:

<style type=&quot;text/css&quot;>
#footer {
visibility: hidden;
}
</style>

Now, when the browser has finished loading the page (and whenever the browser window is resized), we want to find out which of the columns is the tallest and resize them all to that height. Then we can display the footer.

Because this process may happen repeatedly as the user resizes the browser window, we need to wrap the content of each column in an additional <div>. The structure of the document becomes:

<div id=&quot;left&quot;>
<div
id=&quot;leftcontent&quot;><!--left--></div>
</div>
<div id=&quot;content&quot;>
<div
id=&quot;contentcontent&quot;><!--content--></div>
</div>
<div id=&quot;right&quot;>
<div
id=&quot;rightcontent&quot;><!--right--></div>
</div>

It is these &quot;inner&quot; <div> we will check for the natural height of each column before setting the height of the &quot;outer&quot; <div>.

Here's the JavaScript function that adjusts the layout using the X library's xHeight and xShow functions:

<script type=&quot;text/javascript&quot;>
function adjustLayout()
{
// Get natural heights
var cHeight = xHeight(&quot;contentcontent&quot;);
var lHeight = xHeight(&quot;leftcontent&quot;);
var rHeight = xHeight(&quot;rightcontent&quot;);

// Find the maximum height
var maxHeight =
Math.max(cHeight, Math.max(lHeight, rHeight));

// Assign maximum height to all columns
xHeight(&quot;content&quot;, maxHeight);
xHeight(&quot;left&quot;, maxHeight);
xHeight(&quot;right&quot;, maxHeight);

// Show the footer
xShow(&quot;footer&quot;);
}

All that's left is to make this function run when the page is loaded or resized. This is done with xAddEventListener:

window.onload = function()
{
xAddEventListener(window, &quot;resize&quot;,
adjustLayout, false);
adjustLayout();
}
</script>&quot;

Can find some good info there.

Max

 
Follow up - I've found that this technique has an issue with preloading images behavior - will cause the columns not to equalize. I have some more tweaking to do - you can see my testing area for this at

and the css at
Still have some behaviors to add for the drop-down navigation to get out of the way when not needed, but let me know how this works in different browsers/platforms, please.

Will be working on a different version I found at that looks quite promising and uses no javascript - perhaps there won't be the preload interference then.

Max

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top