tags:

views:

44

answers:

3
+2  Q: 

Float and margin

I need to know why the following code:

<!doctype html>
<html>
    <head>
        <title></title>
        <style type="text/css">
            *
            {
                margin:0px;
                padding:0px;
            }
            #right
            {
                float:right;
            }
            #content
            {
                margin-top:20px;
            }
        </style>
    </head>
    <body>
        <div id="right">a</div>
        <div id="content">b</div>
    </body>
</html>

Applies the 20px margin top also at the #right div.

A: 

It works fine when you:

#content {
    margin-top: 20px;
    clear: right;
}
petsagouris
I know how to fix it but I'm intrested in the cause of this. Thanks.
the cause is because you aren't clearing the floated element.
Ross
+1  A: 

it doesn't, strictly. (the margin isn't applied to #right) floating takes the element out of the flow of the document.

add clear:right to #content

and floated elements ~should~ have a width.

Ross
Thanks, I know that floating takes the element out of the flow. Because of this, my floating element should be on the top right, without margin.
ah, the why rather than the how. frankly i couldn't give you a concise definition, im sure someone else can. the floated element does not have the margin. it visually may appear to, but thats because the floated element appears after non-floated content has been displayed or until the float is cleared. thats my understanding at least. you are seeing the margin of #content, and then after #content, #right is output, for lack of a better word.
Ross
the why is more about the way that block versus inline-block versus inline works.
drachenstern
@drachenstem: Thanks. Can you explain me a little bit more, please? I was expecting that the #rightelement was at 0px to top and the #content was at 20px to top.
A: 

Two things are missing: 1) clear:right; within #content. 2) widths need to be specified so that you are able to apply clear:right without the div's stacking.

<html>
    <head>
        <title></title>
        <style type="text/css">
            *
            {
                margin:0px;
                padding:0px;
            }
            #right
            {
                float:right;
                width:24%;
                border:1px solid red;
            }
            #content
            {
                margin-top:20px;
                width:74%;
                clear: right;
                border:1px solid aboue;
            }
        </style>
    </head>
    <body>
        <div id="right">a</div>
        <div id="content">b</div>
        <div style="clear:both;"></div>
    </body>
</html>

I've added the borders so it is easier to view.

Jon Harding
Thank you very much. I'd need to know why, in my version, the floating elements has the margin-top, assigned to #content.
I guess I'm not following because your <styles> specifically add a margin-top:20px to #content
Jon Harding
@Jon Harding ~ the margin-top isn't applied to the `#right` element. He wants to know why the `#right` element has a margin-top. The reason is because of `block` vs `inline-block` vs `inline`
drachenstern