Floats and Margin Collapse

徘徊边缘 提交于 2020-04-17 19:32:14

问题


so I'm having a hard time understanding the circumstances under which a float can have margins collapse through it and how that affects the position of the float. I've included a page that seems to show two different behaviors in the same page.

The red float seems to be positioned before the margins that collapsed through it, whereas the blue ones down below seem to have the margins collapse through them and then be positioned after those margins have collapsed.

Any help would be much appreciated.

Cheers, Ben

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Inheritance Tests</title>
        <style>
        * { 
            margin: 0px ;
            padding: 0px ;

            font-family: courier ;
            font-size: small ;
        }

        .test4 {width: 200px; height: 100px; border-style: solid; border-width: 1px;}
        .test5 {border: 1px solid red; width: 200px; height: 50px; float: left; }

            .floatedRect {
                width: 50px ;
                height: 50px ;
                float: left ;
                background-color: #9cf ;
            }

            .text {
                margin: 10px ;
            }
        </style>
    </head>
    <body>
        <div class="test4">
            Normal Flow
        </div>

        <div class="test5">
            Floated
        </div>

        <div style="margin-top: 100px">
            Has a top margin
        </div>

        <div style="clear: both">
            <div class="floatedRect"></div>
            <div class="text">some text</div>
            <div class="floatedRect"></div>
            <div class="text">some text</div>
            <div class="floatedRect"></div>
            <div class="text">some text</div>
        </div>
    </body>
</html>

回答1:


First your code is made easy to understand. http://jsfiddle.net/gD9PL/

Now your answer, it has nothing to do with topic of floats or margin collapsing at all.

Actually what you see that blue divs are going downwards its because of the reason that their containing div is altogether pushed downward by second div in the line which is text div and it's having margin top.

If you put a border of 1px then you see a different that blue divs (which are floated not pushed the way it was with 0px border containing div). http://jsfiddle.net/gD9PL/1/




回答2:


I'm not sure I completely understand the question, but I'll take a stab at it:

I don't believe that there is a time where a float will collapse a margin. I've looked at your code and I don't see any margin set in the css that then is 'collapsed' when I view it in a browser (I'm using Safari).

Here's my thoughts:

In the first part of your example, you have the normal flow div before the floated div. The floated div will just be rendered below the normal flow div.

In the second part of your example you have the floatedRect divs above the normal flow divs. That's when you see the text move to the right. This is not affecting the margin of the text divs or collapsing the margins. It is just allowing the text to 'float' around the floatedRect divs. I've changed your code to illustrate:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Inheritance Tests</title>
    <style>
    * { 
        margin: 0px ;
        padding: 0px ;

        font-family: courier ;
        font-size: small ;
    }

    .test4 {width: 400px; height: 100px; border-style: solid; border-width: 1px;}
    .test5 {border: 1px solid red; width: 200px; height: 50px; float: left; margin:10px; }

        .floatedRect {
            width: 50px ;
            height: 50px ;
            float: left ;
            background-color: #9cf ;
        }

        .text {
            margin: 10px; border:1px solid red; position:relative; z-index:1;
        }
    </style>
</head>
<body>


    <div class="test5">
            Floated
    </div>

     <div class="test4">
            Normal Flow
    </div>

    <div style="margin-top: 100px">
        Has a top margin
    </div>

    <div style="clear: both">
        <div class="floatedRect"></div>
        <div class="text">some text</div>
        <div class="floatedRect"></div>
        <div class="text">some text</div>
        <div class="floatedRect"></div>
        <div class="text">some text</div>
    </div>
</body>

Notice that the text divs have a 10px margin still, but the text has been pushed to the right by the floatedRect divs.

Hope that helps.




回答3:


Maybe chapter 8.3.1 Collapsing margins in the CSS 2.1 Specification can be of help?




回答4:


Andy Budd wrote an article about CSS and collapsing margins:

http://andybudd.com/archives/2003/11/no_margin_for_error/

Even though dated in 2003, the basic principles still apply. I found the article to be a useful refresher.



来源:https://stackoverflow.com/questions/1705055/floats-and-margin-collapse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!