views:

2689

answers:

8

I want a two column div layout, where each one can have variable width e.g.

<div style="float:left">Tree</div>
<div style="float:left">View</div>

I want 'view' div to expand to whole width available after 'tree' div has filled needed space. Currently my 'view' div is resized to content it contains It will also be good if both divs take up whole height

Not duplicate disclaimer:

http://stackoverflow.com/questions/1017880/expand-div-to-max-width-when-floatleft-is-set because there left one has fixed width

http://stackoverflow.com/questions/727012/help-with-div-make-div-fit-the-remaining-width because I need two columns both aligned to left

A: 

Im not sure if this is the answer you are expecting but, why don't you set the width of Tree to 'auto' and width of 'View' to 100% ?

Because that will put the second float below the first because it's too wide.
cletus
+4  A: 

This would be a good example of something that's trivial to do with tables and hard (if not impossible, at least in a cross-browser sense) to do with CSS.

If both the columns were fixed width, this would be easy.

If one of the columns was fixed width, this would be slightly harder but entirely doable.

With both columns variable width, IMHO you need to just use a two-column table.

cletus
yes looks like two column table is simplest way
Anurag Uniyal
A: 

If both of the widths are variable length why don't you calculate the width with some scripting or server side?

<div style="width: <=% getTreeWidth() %>">Tree</div>

<div style="width: <=% getViewWidth() %>">View</div>
amischiefr
How does the server know how the client's layout engine will work?
Kev
Simple: develop it for 90% of the browsers out there :) IE 7+ , FF3+ and Safari support CSS 3. You could also include the standard IE6 <!--[if lte IE 6]> element. By the way, what browser doesn't support style="width: %" ??
amischiefr
I don't know how It can be done server side, but yes some javscript may do it but i want to avoid it unless css/html can't do it
Anurag Uniyal
Which it can't. Therefore, if you want both to be dynamic then you have to do it in either script or server side.
amischiefr
A: 

Have a look at the available CSS layout frameworks. I would recommend Simpl or, the slightly more complex, Blueprint framework.

If you are using Simpl (which involves importing just one simpl.css file), you can do this:

<div class="Colum­nOne­Half">Tree</div>
<div class="Colum­nOne­Half">View</div>

, for a 50-50 layout, or :

<div class="Colum­nOne­Quarter">Tree</div>
<div class="Colum­nThreeQuarters">View</div>

, for a 25-75 one.

It's that simple.

will both columns be variable width?
Anurag Uniyal
+2  A: 

Here, this might help...

<html>
<head>
<style type="text/css">
div.box { background: #EEE; height: 100px; width: 500px; }
div.left { background: #999; float: left; height: 100%; width: auto; }
div.right { background: #666; height: 100%; }
div.clear { clear: both; height: 1px; overflow: hidden; font-size:0pt; margin-top: -1px; }
</style>
</head>
<body>
<div class="box">
   <div class="left">Tree</div>
   <div class="right">View</div>
   <div class="clear" />
</div>
</body>
</html>
a6hi5h3k
+1 because your example seems to work, but in my real scenario some how contents from 'view' creep into 'tree' div, as tree div is not 100%, may be some javascript forces it to be small and after height of tree I see view contents
Anurag Uniyal
In that case, if u know the max-width of the left column, u can either give it a style max-width (does not work in IE) or think of margin-left (beware of double-margin bug on IE) or padding-left.
a6hi5h3k
+16  A: 

The solution to this is actually very easy, but not at all obvious. You have to trigger something called a "block formatting context", which interacts with floats in a specific way.

Just take that second div, remove the float, and give it overflow:hidden instead. Any overflow value other than auto makes the block it's set on become a BFC. BFCs don't allow descendant floats to escape them, nor do they allow sibling/ancestor floats to intrude into them. The net effect here is that the floated div will do it's thing, then the second div will be an ordinary block, taking up all available width except that occupied by the float.

This should work across all current browsers, though you may have to trigger hasLayout in IE6 and 7. I can't recall.

Xanthir
+1: excellent explanation.
BalusC
+1, sounds nice, can you add an example
Anurag Uniyal
Thank you for such a clear, concise explanation - a whole new door of CSS understanding has just been opened.
Rabid
Perfect - thanks
Robert Morgan
A: 

Thanks for the plug of Simpl.css!

remember to wrap all your columns in .ColumnWrapper like so.

<div class="ColumnWrapper">
    <div class="Colum­nOne­Half">Tree</div>
    <div class="Colum­nOne­Half">View</div>
</div>

I am about to release version 1.0 of Simpl.css so help spread the word!

Shaggy
A: 

Thanks, Xanthir, your insight is worth a ton of gold!

Milen Kirilov