8

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?

2
  • CSS may not be the only tool for the job when other tools lend themselves better to a problem. HTML Tables aren't evil; although they might have been overused in the past before good CSS support, Tables still have their place. When you're talking equal heights, Tables automatically have that and can still be controlled through CSS, for a hybrid solution using best of both worlds. Otherwise it can get complicated fast if using one tool to exclusion of others for the sake of it. Commented Jan 31, 2010 at 3:45
  • 1
    Then again author did ask for a CSS only solution so maybe trying to enforce our ideals to the exclusion of directly answering the question is working against the process. Commented Jan 31, 2010 at 4:06

3 Answers 3

4

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:

Sign up to request clarification or add additional context in comments.

3 Comments

what are cons to use display:table on a div?
@Jitendra - IE 6/7 doesn't support display:table.
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.
0

it can be done easily with <table>

7 Comments

Unless he's displaying tabular data, that's Bad Idea (tm)
@Bryan Ross - Doing loops in the air to achieve something with a simple & existing tool is usually a bad idea in programming. Besides, divs are even less semantic than tables.
Yes, easily done with table + enhanced with sensible CSS to achieve the desired outcome.
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.
|
0

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

1 Comment

+1 for the second link .. it does not have a lot of silly markup .. an extra wrapping div per column ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.