How do you keep parents of floated elements from collapsing?

This question already has an answer here:

  • What methods of 'clearfix' can I use? 28 answers

  • Solution 1:

    The most reliable and unobtrusive method appears to be this:

    Demo: http://jsfiddle.net/SO_AMK/wXaEH/

    HTML:

    <div class="clearfix">
        <div style="float: left;">Div 1</div>
        <div style="float: left;">Div 2</div>
    </div>​
    

    CSS:

    .clearfix::after { 
       content: " ";
       display: block; 
       height: 0; 
       clear: both;
    }
    

    ​With a little CSS targeting you don't even need to add a class to the parent DIV .

    This solution is backwards compatible to IE8 so you don't need to worry about older browsers failing.

    Solution 2:

    An adaptation on solution 1 has been suggested and is as follows:

    Demo: http://jsfiddle.net/wXaEH/162/

    HTML:

    <div class="clearfix">
        <div style="float: left;">Div 1</div>
        <div style="float: left;">Div 2</div>
    </div>​
    

    CSS:

    .clearfix::after { 
       content: " ";
       display: block; 
       height: 0; 
       clear: both;
       *zoom: expression( this.runtimeStyle['zoom'] = '1', this.innerHTML += '<div class="ie7-clear"></div>' );
    }
    
    .ie7-clear {
        display: block;
        clear: both;
    }
    

    This solution appears to be backwards compatible to IE5.5 but is untested.

    Solution 3:

    It's also possible to set display: inline-block; and width: 100%; to emulate a normal block element while not collapsing.

    Demo: http://jsfiddle.net/SO_AMK/ae5ey/

    CSS:

    .clearfix {
        display: inline-block;
        width: 100%;
    }
    

    This solution should be backwards compatible to IE5.5 but has only been tested in IE6.


    I usually use the overflow: auto trick; although that's not, strictly speaking, the intended use for overflow, it is kinda related - enough to make it easy to remember, certainly. The meaning of float: left itself has been extended for various uses more significantly than overflow is in this example, IMO.


    The problem happens when a floated element is within a container box, that element does not automatically force the container's height adjust to the floated element. When an element is floated, its parent no longer contains it because the float is removed from the flow. You can use 2 methods to fix it:

  • { clear: both; }
  • clearfix
  • Once you understand what is happening, use the method below to “clearfix” it.

    .clearfix:after {
        content: ".";
        display: block;
        clear: both;
        visibility: hidden;
        line-height: 0;
        height: 0;
    }
    
    .clearfix {
        display: inline-block;
    }
    
    html[xmlns] .clearfix {
        display: block;
    }
    
    * html .clearfix {
        height: 1%;
    }
    

    Demonstration :)

    链接地址: http://www.djcxy.com/p/2548.html

    上一篇: 如何使用PHP将HTML + CSS转换为PDF?

    下一篇: 你如何阻止浮动元素的父母崩溃?