tags:

views:

48

answers:

2

I'm working on a three column layout:

<div id="first"></div>
<div id="second"></div>
<div id="third"></div>

I'd like to make it so that the first two columns have a fixed width and that the third column takes up the rest of the space on the page. Just setting percentage widths for everything is an option, but not preferable.

An option I considered:

div#first {position: absolute; left: 0; top: 0; width: 150px;}
div#second {position: absolute; left: 150px; top: 0; width: 450px;}
div#third {margin-left: 600px}

I've used stuff like this in the past, but with limited success. Namely, I find that if there are any floated elements in #first or #second, cleared elements in #third would also clear those in #first and #second. While working without clears is possible, I'd a limitation I would like to avoid if possible.

I realized that my desired behaviour could indeed be produced by the following layout:

<table style="width: 100%">
  <tr>
    <td width=150"><div id="first"></div></td>
    <td width=450"><div id="second"></div></td>
    <td><div id="third"></div></td>
  </tr>
</table>

And while SEO is not an issue on this site, a table as the root node just feels wrong.

+1  A: 

Sounds like you're looking for something like this:

CSS

<style type="text/css">
  #first,#second {
    float: left;
  }

  #first {
    width: 150px;
    border: 1px solid red;
  }

  #second {
    width: 450px;
    border: 1px solid green;
  }

  #third {
    width: auto;
    overflow: auto;
    border: 1px solid blue;
  }
</style>

HTML

<div id="first">Left</div>
<div id="second">Middle</div>
<div id="third">Right</div>
Gert G
Unless I'm mistaken, that won't work as long as there is a block element within `#third` with `width > windowWidth - 600`, since `#third` would simply become too wide and go beneath `#first` and `#second`. This would mean I wouldn't be able to put in a child element with `width: 100%`, either explicit or implied, which is exactly what I want to do. See: http://jsfiddle.net/gvUxS/. Notice that the pink extends all the way across.
Steven Xu
Good catch. I added `overflow: auto;` to `#third`.
Gert G
@Steven Xu - Did this solve your issue? Or is there anything I can do to improve the answer?
Gert G
Sorry I couldn't get back to you sooner. `overflow: auto` seems to do the trick. I've never encountered this use of the overflow property before; I've only ever used it to make scrollbars appear. According to the specification, `overflow: visible` seems to make the element a flow root. `auto` avoids this.
Steven Xu
@Steven Xu - `overflow: auto;` essentially tells the browser to do what it thinks is right (e.g. should there be a scrollbar or should the content just expand etc.). Thanks for accepting the answer. :)
Gert G
A: 

with a little javascript, its perfect/ add jquery first for selectors & etc.

<script type="text/javascript" src="js/jquery.js"></script>

and the javascript part/

$(document).ready(function(){
    $('#third').css("width",$(window).width()-600)

    $(window).resize(function() {
        $('#third').css("width",$(window).width()-600)
    });
});
spielersun