tags:

views:

177

answers:

2

I'm trying to get two Divs to sit side by side. I want one div to take up as much width as is needed and the other to to take up the remaining width so both divs span 100% width. Is this possible? I've tried floating and a bunch of different positioning settings but I can't find a solution that works. I naturally thought that adding a float: left to the left most element would work, however when you try to add padding/margin/border to the right element the browser wont apply it. Here is some code that I've extended (from an existing answer) to illustrate the problem.

<style>
    #foo {
        float: left;
        background: red;
        height: 100%;
        padding: 5px;
    }
    #bar {
        background: green;
        border: solid 1px blue;
        padding: 5px;
        height: 100%;
    }
</style>   
<div>
    <div id="foo">foo</div>
    <div id="bar">bar</div>
</div>

If you open this code up in a browser you'll notice that the bar div isn't padded, and the border isn't applied to it... I have no idea why.

Thanks for any help.

+1  A: 

why not use a table, set the whole table width to 100% and then each of the rows without a width, like so:

<table width="100%" border="0"> <tr> <td><div>DIV INFO LEFT</div></td> <td><div>DIV INFO RIGHT</div></td> </tr> </table>

Patrick Gates
Using table is not a good way! ;)
Ehsan
Using a table is not a *semantic* way. But even today many web developers don't really care about semantic markup. We cannot say that this is a *bad* way if it works, it's only a *deprecated and ugly* way.
MvanGeest
I've tried this too. However in the left table cell any text you put in there will be wrapped word for word. Is there a way to prevent the text-wrapping? Thanks for the help.
Brian D.
@MvanGeest how can a way be deprecated and ugly and still not bad?
Agos
That wont wrap it because the columns have no width's set, so it automatically goes with the flow, for lack of a better term, and in my scripting, I've learned not to focus on semantics because when you get into complex PHP scripts, semantics are the first thing to fly out the window.
Patrick Gates
Even though this solution did not end up working for me (I ended up using DIVs and specifying a width for one of them) I marked it as the answer because it is closest solution to the original question I asked.
Brian D.
+2  A: 

This works:

<style>
    #foo {
        float: left;
        background: red;
    }
    #bar {
        background: green;
    }
</style>   
<div>
    <div id="foo">foo</div>
    <div id="bar">bar</div>
</div>

http://pastehtml.com/view/19ldeqq.html

Finbarr
I thought this would work too, however when you try to add padding/margin/border to the bar div, it wont be applied. I've updated my question to reflect this. Thanks for the help.
Brian D.