Skip to content Skip to sidebar Skip to footer

Floats And Margin Collapse

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 p

Solution 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/

Solution 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:

<htmlxmlns="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><divclass="test5">
            Floated
    </div><divclass="test4">
            Normal Flow
    </div><divstyle="margin-top: 100px">
        Has a top margin
    </div><divstyle="clear: both"><divclass="floatedRect"></div><divclass="text">some text</div><divclass="floatedRect"></div><divclass="text">some text</div><divclass="floatedRect"></div><divclass="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.

Solution 3:

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

Solution 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.

Post a Comment for "Floats And Margin Collapse"