tags:

views:

139

answers:

6

Yo. There's a tendency in placing divs to follow each other vertically, but what i'm trying to accomplish right now is to is basically to place a number of divs (two) inside a parent div like so:

<div id='parent'><div id='onediv'></div> <div id='anotherone'></div> </div>

And i'd like to place 'anotherone' just to the right of 'onediv'. Sadly, float:right is pretty much ruining the layout with the divs popping out of their parent divs and whatnot. Any suggestions are welcome.

Edit: It might be worth noting that the parent div and 'anotherone' has no height elements at all, with 'onediv' planned to be thought as the "height support" div, allowing the contents of 'anotherone' to make the parent div larger at will.

Edit again: Here's the CSS for the specified stuff:

.parent
{
width: 90%;
margin: 0 auto;
border:solid black 1px;
}
.firstchild
    {
        width: 20%;
        margin: 5px;
        border: solid black 1px;
        height: 180px;
    }
.secondchild
    {
            width: 60%;
            border:solid black 1px;
            margin: 5px;
    }
A: 
#onediv { float: left; width: 50%; } #anotherone { float: right; width: 50%; }
Kevin
Doesn't work, that's the way I tried it before, the divs pop out of their parent divs and go all over the place for giggles.
Andreas Carlbom
Works for me...
Kevin
+2  A: 

Try this:

<div id="parent">
    <div id="onediv" style="float:left;"></div>
    <div id="anotherone" style="float:left;"></div>
    <div style="clear:both;"></div>
</div>
rossisdead
#onediv now appears to the right of #anotherone...
Finbarr
Sorry, those float:rights were meant to be float:left. I edited my answer to reflect that.
rossisdead
+2  A: 

You can float both inner divs and give the outer div an overflow so that it grows with the inner divs.

Example:

#parent {
    overflow: hidden;
}
#parent div {
    width: 50%;
    float: left;
}
jeroen
This worked. Kool.
Andreas Carlbom
I had no idea that would work! http://www.quirksmode.org/css/clearing.html
rossisdead
There's not a site - where I use floats - where I don´t use this solution. I avoids the - often unwanted - side-effects of `clear:both`, as that can clear a lot more than just the 2 divs above it...
jeroen
You learn something new every day.
Finbarr
+1  A: 

I think this is what you want (note the re-ordering of DOM elements):

<div id="parent">
  <div id="anotherone"></div>
  <div id="onediv"></div>
</div>

/*CSS*/
#anotherone{
  float:right;
  width:50%;
}
#onediv{
  float:left;
  width:50%;
}

Note, if this is what you want, IE6 will still mess it up. ;-)

scunliffe
Bah. Who cares about IE6
Andreas Carlbom
The child divs will still spill out from the parent div here.
Finbarr
it depends on what padding/border/margin etc. are set on them but since that wasn't in the original post, I just tried to guess what the OP was after.
scunliffe
Just add `overflow:hidden;` to `#parent` and it will grow with its contents, see my *downvoted* (???) answer...
jeroen
A: 

Just use the <span> tag. Its the equivalent of except it doesn't start a new row.

teehoo
+1  A: 

You certainly need to specify a width as indicated in @Kevin's answer to get the layout you described, simply specifying float left/right will not have the desired effect. Try specifying the width in pixels rather than a percentage. Failing that or if that's not appropriate for you, I think you possibly need to specify the width of the outer div (through css if you like).

s1mm0t