tags:

views:

84

answers:

4

In CSS, how do I achieve the following layout?

xxxxxxxxxxxxxR1xxxxxxxxxxxxx
xxxxxxxxxxxxxR1xxxxxxxxxxxxx
xxxxxxxxxxxxxR1xxxxxxxxxxxxx

xR2.1x   xxxxxxxxxR2.1xxxxxx
xR2.1x   xxxxxxxxxR2.2xxxxxx
xR2.1x   xxxxxxxxxR2.2xxxxxx

xxxxxxxxxxxxxR3xxxxxxxxxxxxx
xxxxxxxxxxxxxR3xxxxxxxxxxxxx
xxxxxxxxxxxxxR3xxxxxxxxxxxxx

R2.1 is an image that I want to go to the left, and R2.2 is a div that I want to go to the right.

If R2.2 was a paragraph, it would be easy - I could just use float:left on R2.1 - but the fact that it is a div seems to mess things up.

I've tried using <img style="float:left;"> and <div style="float:right">, together and separately, but they don't seem to combine well.

What am I doing wrong?

A: 

Try:

<div style="margin-left: [width of picture]">
jay
div is now the right width, but img is still pushed down below it on the next line?
AP257
have done but still being pushed down, please see: larochedenchaille.com/guest-reviews
AP257
A: 

The floats should work as expected. Make sure you add a clear:both to the R3 div, so it knows it should start beneath the floating divs, and that the left/right divs have the correct widths so they aren't pushed to a new line. Example:

<div>r1</div>
<img style="float:left;" alt="r2.1" />
<div style="float:right;">r2.2</div>
<div style="clear:both;">r3</div>​​​​​​​​​​​​​​​​

http://jsfiddle.net/wHm8n/

Alec
Think the widths are the problem. the img is a fixed width of 180px, how can I set the div to '100%-180px'?
AP257
+1  A: 

Is there any reason you can't put the image inside the second div? That would sort it out:

<div id="r1" style="height:100px; background-color:#eff;">r1r1r1r1r1r1r1r1r1r1</div>
<div id="r2" style="height:100px; background-color:#efe;">r2r2r2r2r2r2r2r2r2r2
   <div id="image" style="width:160px;height:100px;background-color:#ff8;float:left">
   </div>
</div>
<div id="r3" style="height:100px; background-color:#fee;">r3r3r3r3r3r3r3r3r3r3</div>

http://jsfiddle.net/WP6bc/1/

david
I'd like the div to have a pale blue background, and the image to be against the white background of the page... and I'd like the div not to wrap round the image... and I want to understand how to float two divs separately, as in the question. May have to go with your solution if I can't work this out though!
AP257
It's kinda funny watching you change the page as I update.You came really close at one stage, I think part of your problem is Div R2.2 has a margin of 10px at the top and the bottom, but the image only has the margin on the sides. Add the 10px margin to the top and the bottom of the image and it will line up better
david
got it now... sorry about that, thank you for your help :)
AP257
+1  A: 

There is no need to use float:right on the right side, just use float:left and margin:

<div>r1</div>
<img style="float:left; width:50px; height:50px;" title="r2.1" />
<div style="margin-left:50px;">r2.2</div>
<div style="clear:both;">r3</div>​
markt
gets pushed down, see: http://larochedenchaille.com/guest-reviews
AP257
no - it finally works! thank you!
AP257