tags:

views:

66

answers:

3

I have two columns side by side that are different colors. The background has a unique color as well. The right column contains text that will expand to an unknown height. The other column contains little to nothing.

<div id="container">
    <div id="leftColumn">
        <p>Only one small paragraph here</p>
    </div>

    <div id="rightColumn">
        <p>Many large paragraphs inside here.</p>
    </div>
</div>

I would like the left column to be the exact height as the right column.

Here's the CSS...

#leftColumn {
    display:inline-block;
    width: 200px;
}
#rightColumn {
    display:inline-block;
    width: 600px;
    vertical-align: top;
}

So when the page loads I use jQuery to set the height of the left column based on the height of the right column.

$(document).ready(function() {
    $('#leftColumn').css('height', $('#rightColumn').innerHeight());
});

Is there a way to do this with only CSS?

A: 

it can be done easily with <table>

Itay Moav
Unless he's displaying tabular data, that's Bad Idea (tm)
Bryan Ross
Downvoted (sorry) because it's a bad advice.
metrobalderas
Itay Moav
@metrobalderas and why is it a bad idea?
Itay Moav
Yes, easily done with `table` + enhanced with sensible CSS to achieve the desired outcome.
John K
As easy as using tables would be, I don't think I'm going to go that route. Just going off what I've read - tables are for data. Browsers interpret the table tag as something specifically designed for data. By using tables for something other than data, you either a) tie the hands of browser developers OR b) risk having your site break because a browser developer decided to render tables differently in the future.Plus disregard for the W3C results in things like IE.
The real solution should be an update to CSS than handles height like width. Either an extension or another value to the height attribute. Until then we make do. And @Itay Moav, how exactly are divs less semantic? He wants to "divide" the page into two columns.
Bryan Ross
@dgendill , browsers DON'T interpret content of tables in any specific way, may be Google is, I doubt it, as many sites won't be indexed in that case.table IS the cross browser ONLY solution and WILL be the ONLY method to be parsed the same in future browsers (as oposed to CSS and JS solutions).
Itay Moav
A: 

I am not sure if there's a pure CSS way to do this (maybe in CSS3). But you should read this article and see if it works for you.

http://www.alistapart.com/articles/fauxcolumns/

If not, you could consider this technique (which requires a LOT of silly markup).

http://matthewjamestaylor.com/blog/equal-height-columns-3-column.htm

jessegavin
Why the downvote? What the heck?
jessegavin
+1 for the second link .. it does not have a lot of silly markup .. an extra wrapping div per column ..
Gaby
+2  A: 

There are a few other ways to achieve this layout besides using Javascript.

Methods include:

  • Using display:table on the elements
  • Faux columns (background image on the parent element)
  • Adding multiple div containers for each background
  • Use a table (not very popular for obvious reasons)

All of these have different advantages, drawbacks and each introduces thier own headaches to the development of the site. I'd vote for using faux columns because it keeps the html the simplest and is compatible with all browsers.

Additional reading:

Lance McNearney
what are cons to use display:table on a div?
metal-gear-solid
@Jitendra - IE 6/7 doesn't support display:table.
Lance McNearney
Thanks for that nice list of options. I'll have to consider what's going to be best for my design. Options three looks like something that might work well. The faux columns also looks good, but I'm not sure if I want to deal with the box-model differences between IE and other browsers.