tags:

views:

73

answers:

3

Hello everyone,

I have two DIVs, .sidebar and .content and I want to set .sidebar to keep the same height with the .content.

I've tried the following:

$(".sidebar").css({'height':($(".content").height()+'px'});

$(".sidebar").height($(".content").height());

var highestCol = Math.max($('.sidebar').height(),$('.content').height());
$('.sidebar').height(highestCol);

None of these are working. For the .content I don't have any height since will increase or decrease based on the content.

Please help me. I have to finish up a web page today and this (simple) thing is giving me headaches.

Thank you!

+1  A: 

I think what you're looking for is faux columns.

Victor Welling
+2  A: 

In my experience, it's a very, very bad idea to use Javascript for this sort of thing. The web should be semantic. It isn't always, but it should. Javascript is for interactive functionality. HTML is for content. CSS is for design. You CAN use one for the the other's purpose, but just because you CAN do something doesn't mean you SHOULD.

As for your problem specifically, the short answer is: you don't stretch the sidebar. You don't have to. You set up a background that looks like your sidebar and let it look like a column. It's, as Victor Welling put it, a faux-column.

Here's one of many web pages that show how to do it:

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

But resort it to Javascript for that sort of presentation issue would be "wrong" even if it worked.

eje211
I know this method, but what if you have already applied another background so you still need to use jQuery?
George
What other background? Can you give an example. You should never *need* to use jQuery for this. In the example on the page that Victor and I linked to, there is a single background that LOOKS like two columns.In all likelihood, your sidebar is in a container. The container should look like your sidebar. Or you could double the container and set the sidebar's background in the innermost container, with the sidebar's width.Another way of proceeding is to use a free template and to modify it. If you observe how the template does its job, and the template does it well, you might learn a lot.
eje211
A: 

eje211 has a point about using CSS to style your page. Here are two methods to use CSS, and one method using scripting to accomplish this:

  1. This article makes equal column heights without CSS hacks.

  2. Here is a method to get equal column heights using a CSS hack.

  3. and lastly, if you do want to use javascript/jQuery, you could use the equalize heights script that the jQuery .map() page has as a demo.

    $.fn.equalizeHeights = function(){
      return this.height( Math.max.apply(this, $(this).map(function(i,e){ return $(e).height() }).get() ) )
    }
    

    then just use it as follows:

    $('.sidebar, .content').equalizeHeights();
    
fudgey