views:

150

answers:

3

Hi, I'm trying to put two divs on the right side of parent div one above another. Something like this:

 div#top 
|-------------------------------------||------------|
|div#parent                           ||div#menu    |
|                          |---------|||float:right |
|                          |div#up   |||            |
|                          |         |||            |
|                          |---------|||------------|
|                                     |
|                          |---------||
|                          |div#down ||
|                          |         ||
|                          |---------||
|-------------------------------------|

Any ideas how to do it using css? I can't use tables here because div#up is added in Master page (ASP.NET) and div#down is added in content. Div#parent is liquid with some min-width set and contains content that should not be overlapped by these divs so i think position:absolute is out of question here too.

One more detail: on the left and right of div#parent there ale divs floated left and right with menus. So adding clear:left/right to div#down floated right puts it under one of these menus...

+2  A: 

Personally I would wrap them in a container div, and give it a width and float it right.

#sidebar{
  width: 250px;
  float: right;
}
DavidYell
He cannot add another div around those two as they are added by different components of the system. That's why a table solution is out of question for him.
buti-oxa
A: 

You need to use both float:right and clear:right, which ensures that the right-hand side of the element is unobstructed to the edge of the containing element.

<div id="parent" style="width: 80%">

    <div id="up"   style="float: right; clear: both;">div with id 'up'</div>
    <div id="down" style="float: right; clear: both;">div with id 'down'</div>
    'parent' div

</div>
ar
That is almost working, but as I wrote I have floated menus to the left and right and using clear causes my divs to go under them. Is there any way to prevent this behavior?
Episodex
You probably just need to change the CSS to only use `clear: right;`
ar
+2  A: 

You need to make sure that the parent block (#parent in your case) becomes the containing block block formatting context of the divs #up and #down, so that any clearing only happens inside that containing block block formatting context and ignores the floats outside of it. The easiest way to do this usually is to either let #parent float, too, or give it an overflow other than visible.

http://www.w3.org/TR/CSS2/visudet.html#containing-block-details http://www.w3.org/TR/CSS2/visuren.html#block-formatting

Here some proof, that I got it right this time: http://jsfiddle.net/Pagqx/

Sorry for the confusion.

RoToRa
It's the first time I hear of "clearing only happening inside containing block" and spec (your link) says nothing about it. Is that a behavior that is actually implemented in browsers?
buti-oxa
Unfortunately that doesn't seem to work. Divs fall down under the menus.
Episodex
@buti-oxa: Here: http://www.w3.org/TR/CSS2/visuren.html#propdef-clear Quote. "The 'clear' property does not consider floats inside the element itself or in other block formatting contexts."
RoToRa
Hmm, sorry, my error. I confused "containing block" and "block formatting context". I'll change my answer...
RoToRa
Works like a charm! Thank you.
Episodex