1

I have some html code that look like this sample here :

<html>
  <body>
    <div id="A" style="float:right; Background-color: Red;">Alpha</div>
    <div id="B" style="margin-top: 20px; Background-color: Green;">Bravo</div>
  </body>
</html>

What I'd like to achieve is have the A div in the upper right corner and the B div aligned on the left 20 pixels from the top of the page. As you can see below, it does exactly what I want in IE8, but it doesn't quite works in Chrome and Firefox.

enter image description here

Because it's often the case, I suppose Chrome and FF are right to render it this way and IE8 is not following the standard. So my question is how can I get the desired result (What IE does in the SS) across all browser?

EDIT1&2: I edited the screenshot to show that I really want the Bravo div to be on the left of the Alpha div but 20px lower and not under it like it would if I simply add a clear: right on Bravo.

Thanks

Mathieu Pagé
  • 10,764
  • 13
  • 48
  • 71

4 Answers4

1

EDIT
Answer to the new question:

<div id="A" style="float:right; background-color: Red; margin-top: -5px;">Alpha</div>
<div id="B" style="margin-top: 20px; background-color: Green;">Bravo</div>

ORIGINAL
(The question author originally asked this and later changed the question...)

Add clear: right; into B div's style. Optionally use clear: both;. And wrap div A into a wrapper div like this:

<div id="wrapper" style="overflow: hidden;">
    <div id="A" style="float:right; background-color: Red;">Alpha</div>
</div>
<div id="B" style="margin-top: 50px; background-color: Green; clear: right;">Bravo</div>
Jarno Argillander
  • 5,885
  • 2
  • 31
  • 33
0

Try this:

<html>
  <body>
    <div id="A" style="float:right; Background-color: Red;">Alpha</div>
    <div id="B" style="float:right; margin-top: 50px; Background-color: Green; clear:right; width:100%;">Bravo</div>
  </body>
</html>

Added:

float:right; clear:right; and width:100%; to #B

See it in action - http://jsfiddle.net/SHubq/

Alex
  • 8,875
  • 2
  • 27
  • 44
  • I've edited my SS and question to specify that I really need Alpha to be floated on the right of Bravo. I'm sorry that I did not do so in my original question. – Mathieu Pagé Nov 16 '11 at 15:43
0

You have to add the clear CSS rule.

  • add between your two divs

    <div style="clear: both" />. (Note : in your case, just clear: right works)

or

  • add clear: both in the div id="B" style. (Note : in your case, just clear: right works)

Edit : See the two cases in this fiddle http://jsfiddle.net/dyGyu/

Jean-Charles
  • 1,690
  • 17
  • 28
0

Add a clear property onto b's div style, use the property that is most appropriate to the site in question.

Such as both so you get nothing either side.

<html>
  <body>
    <div id="A" style="float:right; Background-color: Red; ">Alpha</div>
    <div id="B" style="margin-top: 50px; Background-color: Green; clear: both;">Bravo</div>
  </body>
</html>