zoukankan      html  css  js  c++  java
  • 清除浮动(闭合浮动元素)方法总结

    什么是CSS清除浮动?

    网络上流行的说法是:在非IE浏览器(如Firefox)下,当容器的高度(height) 为auto,且容器的内容中有浮动(float为left或right)的元素,在这种情况下,容器的高度不能自动伸长以适应内容的高度,使得内容溢出到 容器外面而影响(甚至破坏)布局的现象。这个现象叫浮动溢出,为了防止这个现象的出现而进行的CSS处理,就叫CSS清除浮动。

    出现浮动溢出的条件是什么?

    首要条件:height:auto,或者说高度是自动伸展的。

    由于容器里面有float元素,由于float元素是脱离文档流的,所以容器是不能获取float的高度的。看下面的例子:

    <style>
    #content{
        *width:100px;
        border:2px solid lime;
    }
    #left1{
        width:100px;
        height:200px;
        border:5px solid red;
        float:left;
    }
    #left2{
     
        margin-left:120px;
        
        width:200px;
        height:100px;
       
        background:#ccc;
    }
    </style>
    <body>
        <div id="content">
            <div id="left1"></div>
            <div id="left2"></div>
       
        </div>
    
    </body>

    显示为:

    于left1为float元素,虽然height为200px,但我们的容器只是包围了未浮动的元素,而没有包围float元素。所有我们需要

    闭合浮动元素。更常见的情况是容器里面都是浮动元素,导致容器的高度坍塌为0.

     

    CSS的浮动为我们的布局带来了很大的好处,利用浮动我们可以很方便的写出各种版式。但浮动同时也带来很多问题;这些问题基本上都因为没有及时的或者在适当的位置清楚浮动。下面来说下常用的清除浮动方法:

    一:用空标签

    这是最常见的一种方法了。通过一个内容为空的标签来清除浮动。这个标签可以是DIV、P、Hr…。理论上可以是任何标签。

    这种方式是在需要清除浮动的父级元素内部的所有浮动元素后添加这样一个标签清楚浮动,并为其定义CSS代码:clear:both。这种方法的弊端在于增加了无意义的结构元素。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head profile="http://gmpg.org/xfn/11">
    <title>CSS清除浮动的一些方法</title>
    <style type="text/css">
    <!--
        *{margin:0;padding:0;}
        body{font:36px bold; color:#F00; text-align:center;}
        #layout{background:#FFC;}
        #left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;}
        #right{float:right;width:30%;height:80px;background:#DDD;line-height:80px;}
        .clear{clear:both;}
    -->
    </style>
    </head>
    <body>
    <div id="layout">
        <div id="left">Left</div>
        <div id="right">Right</div>
        <div class="clear"></div>
    </div>
    </body>
    </html>

     在有浮动的元素的时候一定要清除浮动,并且清除浮动一定要写在被浮动的最后一个元素的后面,要不然也是起不到任何作用的。

    原理:添加一个空div,利用css提高的clear:both清除浮动,让父级div能自动获取到高度(清除浮动的一种方法:设置父级div的height,很少用)。

    优点:简单,代码少,浏览器支持好,不容易出现怪问题

    缺点:不少初学者不理解原理;如果页面浮动布局多,就要增加很多空div,让人感觉很不爽

    建议:不推荐使用,但此方法是以前主要使用的一种清除浮动方法

     

    二:使用overflow属性 overflow:auto;

    这种方法有效地解决了通过添加空标签元素清除浮动而不得不增加无意代码的弊端。使用该方法是只需在需要清除浮动的元素中定义CSS属性:”overflow:auto”,即可!为了兼容IE6同时要加上”zoom:1″。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head profile="http://gmpg.org/xfn/11">
    <title>CSS清除浮动的一些方法</title>
    <style type="text/css">
    <!--
        *{margin:0;padding:0;}
        body{font:36px bold; color:#F00; text-align:center;}
        #layout{background:#FF9;overflow:auto;zoom:1;}
        #left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;}
        #right{float:right;width:30%;height:80px;background:#DDD;line-height:80px;}
    -->
    </style>
    </head>
    <body>
    <div id="layout">
        <div id="left">Left</div>
        <div id="right">Right</div>
    </div>
    </body>
    </html>

    优点:不存在结构和语义化问题,代码量极少

    缺点:多个嵌套后,firefox某些情况会造成内容全选;IE中 mouseover 造成宽度改变时会出现最外层模块有滚动条等,firefox早期版本会无故产生focus等, 请看 嗷嗷的 Demo ,不要使用

    原理:必须定义width或zoom:1,同时不能定义height,使用overflow:hidden时,浏览器会自动检查浮动区域的高度.

    (我们首先看overflow含义:

    根据CSS的盒模型概念,页面中的每个元素,都是一个矩形的盒子。这些盒子的大小、位置和行为都可以用CSS来控制。对于行为,我的意思是当盒子内外的内容改变的时候,它如何处理。比如,如果你没有设置一个盒子的高度,该盒子的高度将会根据它容纳内容的需要而增长。但是当你给一个盒子指定了一个高度或宽度而里面的内容超出的时候会发生什么?这就是该添加CSS的overflow属性的时候了。

    我试着没有把父元素的width设置,只写了overflow:auto也可以)

    优点:简单,代码少,浏览器支持好

    缺点:不能和position配合使用,因为超出的尺寸的会被隐藏。

    建议:只推荐没有使用position或对overflow:hidden理解比较深的朋友使用。

     设置overflow,为了兼容IE,还是加上

    overflow:auto;*zoom:1;。

     三:父级div定义 overflow:hidden

    <style type="text/css"> 
    .div1{background:#000080;border:1px solid red;/*解决代码*/width:98%;overflow:hidden}
    .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px;width:98%}
     
    .left{float:left;width:20%;height:200px;background:#DDD}
    .right{float:right;width:30%;height:80px;background:#DDD}
    </style> 
     
    <div class="div1"> 
        <div class="left">Left</div> 
        <div class="right">Right</div>
    </div>
     
    <div class="div2">
    div2
    </div>

    原理:必须定义width或zoom:1,同时不能定义height,使用overflow:hidden时,浏览器会自动检查浮动区域的高度

    优点:简单,代码少,浏览器支持好

    缺点:不能和position配合使用,因为超出的尺寸的会被隐藏。

    建议:只推荐没有使用position或对overflow:hidden理解比较深的朋友使用。

    四:使用after伪对象清楚浮动。

    该方法只适用于非IE浏览器。具体写法可参照以下示例。
    使用中需注意以下几点:

      1. 该方法中必须为需要清除浮动元素的伪对象中设置height:0,否则该元素会比实际高出若干像素;
      2. content属性是必须的,但其值可以为空,蓝色理想讨论该方法的时候content属性的值设为”.”,但我发现为空亦是可以的
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head profile="http://gmpg.org/xfn/11">
    <title>CSS清除浮动的一些方法</title>
    <style type="text/css">
    <!--
        *{margin:0;padding:0;}
        body{font:36px bold; color:#F00; text-align:center;}
        #layout{background:#FF9;}
        #layout:after{display:block;clear:both;content:"";visibility:hidden;height:0;}
    #layout{ zoom:1; }
    #left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;} #right{float:right;width:30%;height:80px;background:#DDD;line-height:80px;} --> </style> </head> <body> <div id="layout"> <div id="left">Left</div> <div id="right">Right</div> </div> </body> </html>

    注意是父级div定义 伪类:after 。完美的代码如下:

    .container-with-generated-content:after{
        content: ".";
        display: block;
        height: 0;
        clear: both;
        visibility: hidden;
    }
    #
    container-with-generated-content{ zoom:1; }

    在IE中还要加上zoom:1;  zoom(IE转有属性)可解决ie6,ie7浮动问题

    优点:浏览器支持好,不容易出现怪问题(目前:大型网站都有使用,如:腾迅,网易,新浪等等)

     

    五:父级div 也一起浮动

    <style type="text/css"> 
    
    .div1{background:#000080;border:1px solid red;/*解决代码*/width:98%;margin-bottom:10px;float:left}
    
    .div2{background:#800080;border:1px solid red;height:100px;width:98%;/*解决代码*/clear:both}
    
    
    .left{float:left;width:20%;height:200px;background:#DDD}
    
    .right{float:right;width:30%;height:80px;background:#DDD}
    
    </style> 
    
     
    <div class="div1"> 
    
        <div class="left">Left</div> 
    
        <div class="right">Right</div>
    
    </div>
    
     
    
    <div class="div2">
    
    div2
    
    </div>

    原理:所有代码一起浮动,就变成了一个整体。(这个解释不科学《css权威指南》解释:float元素有一个特写:

    float元素会延伸,从而包含其所有后代浮动元素,所有,通过设置将父元素置为浮动,就可以把浮动元素包含在其父元素内)。

    优点:没有优点

    缺点:会产生新的浮动问题。

    建议:不推荐使用,只作了解。

    六: 父级div定义 height

    <style type="text/css"> 
    
    .div1{background:#000080;border:1px solid red;/*解决代码*/height:200px;}
    
    .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}
    
     
    
    .left{float:left;width:20%;height:200px;background:#DDD}
    
    .right{float:right;width:30%;height:80px;background:#DDD}
    
    </style> 
    
     
    
    <div class="div1"> 
    
        <div class="left">Left</div> 
    
        <div class="right">Right</div> 
    
    </div>
    
     
    
    <div class="div2">
    
    div2
    
    </div>

    原理:父级div手动定义height,就解决了父级div无法自动获取到高度的问题。

    优点:简单,代码少,容易掌握

    缺点:只适合高度固定的布局,要给出精确的高度,如果高度和父级div不一样时,会产生问题

    建议:不推荐使用,只建议高度固定的布局时使用

    七:父级div定义 display:table (其实就是table-cell其的作用,因为定义table会生成一个匿名的table-cell)

    <style type="text/css"> 
    .div1{background:#000080;border:1px solid red;/*解决代码*/width:98%;display:table;margin-bottom:10px;}
    .div2{background:#800080;border:1px solid red;height:100px;width:98%;}
     
    .left{float:left;width:20%;height:200px;background:#DDD}
    .right{float:right;width:30%;height:80px;background:#DDD}
    </style> 
     
    <div class="div1"> 
        <div class="left">Left</div> 
        <div class="right">Right</div>
    </div>
     
    <div class="div2">
    div2
    </div>

    原理:将div属性变成表格
    优点:没有优点
    缺点:会产生新的未知问题。
    建议:不推荐使用,只作了解。

    (我试了下,有许多奇奇怪怪的问题,不使用。盒模型属性已经改变,由此造成的一系列问题,得不偿失,不推荐使用

    八:结尾处加 br标签 clear:both

    跟div 加clear:both一样。

    一种新的方法:

    /**
     * For modern browsers
     * 1. The space content is one way to avoid an Opera bug when the
     *    contenteditable attribute is included anywhere else in the document.
     *    Otherwise it causes space to appear at the top and bottom of elements
     *    that are clearfixed.
     * 2. The use of `table` rather than `block` is only necessary if using
     *    `:before` to contain the top-margins of child elements.
     */
    .cf:before,
    .cf:after {
        content: " "; /* 1 */
        display: table; /* 2 */
    }
    
    .cf:after {
        clear: both;
    }
    
    /**
     * For IE 6/7 only
     * Include this rule to trigger hasLayout and contain floats.
     */
    .cf {
        *zoom: 1;
    }

    原理还是一样的。使用 :after 伪类来提供浮动块后的 clear:both。不同的是,隐藏这个空白使用的是 display: table。而不是设置 visibility:hidden;height:0;font-size:0; 这样的 hack。

    值得注意的是这里中的 :before 伪类。其实他是来用处理 top-margin 边折叠的,跟清理浮动没有多大的关系。但因为浮动会创建 block formatting context,这样浮动元素上的另而一元素上如果刚好有 margin-bottom 而这个浮动元素刚好有margin-top 的话,应该让他们不折叠(虽然这种情况并不常见)

    This “micro clearfix” generates pseudo-elements and sets their display totable. This creates an anonymous table-cell and a new block formatting context that means the :before pseudo-element prevents top-margin collapse. The :after pseudo-element is used to clear the floats. As a result, there is no need to hide any generated content and the total amount of code needed is reduced.

    Including the :before selector is not necessary to clear the floats, but itprevents top-margins from collapsing in modern browsers. This has two benefits:

    1. It ensures visual consistency with other float containment techniques that create a new block formatting context, e.g., overflow:hidden
    2. It ensures visual consistency with IE 6/7 when zoom:1 is applied.

    N.B.: There are circumstances in which IE 6/7 will not contain the bottom margins of floats within a new block formatting context. Further details can be found here: Better float containment in IE using CSS expressions.

    The use of content:" " (note the space in the content string) avoids anOpera bug that creates space around clearfixed elements if thecontenteditable attribute is also present somewhere in the HTML. Thanks to Sergio Cerrutti for spotting this fix. An alternative fix is to usefont:0/0 a.

    转自:http://nicolasgallagher.com/micro-clearfix-hack/

    http://www.quirksmode.org/css/clearing.html

    来自:http://leotheme.cn/javascript/css-clear-float-fangfa.html

    http://www.phplover.cn/post/450.html

    一篇好文:

    http://www.iyunlu.com/view/css-xhtml/55.html 哪些年我们清除过的浮动

    精华部分:

     小结

    通过对比,我们不难发现,其实以上列举的方法,无非有两类:

    其一,通过在浮动元素的末尾添加一个空元素,设置 clear:both属性,after伪元素其实也是通过 content 在元素的后面生成了内容为一个点的块级元素;

    其二,通过设置父元素 overflow 或者display:table 属性来闭合浮动,我们来探讨一下这里面的原理。

    在CSS2.1里面有一个很重要的概念,但是国内的技术博客介绍到的比较少,那就是 Block formatting contexts (块级格式化上下文),以下简称 BFC。

    CSS3里面对这个规范做了改动,称之为:flow root,并且对触发条件进行了进一步说明。

    那么如何触发BFC呢?

    • float 除了none以外的值 
       
    • overflow 除了visible 以外的值(hidden,auto,scroll ) 
       
    • display (table-cell,table-caption,inline-block) 
       
    • position(absolute,fixed) 
       
    • fieldset元素

    需要注意的是,display:table 本身并不会创建BFC,但是它会产生匿名框(anonymous boxes),而匿名框中的display:table-cell可以创建新的BFC,换句话说,触发块级格式化上下文的是匿名框,而不是display:table。所以通过display:table和display:table-cell创建的BFC效果是不一样的。

     fieldset 元素在www.w3.org里目前没有任何有关这个触发行为的信息,直到HTML5标准里才出现。有些浏览器bugs(Webkit,Mozilla)提到过这个触发行为,但是没有任何官方声明。实际上,即使fieldset在大多数的浏览器上都能创建新的块级格式化上下文,开发者也不应该把这当做是理所当然的。CSS 2.1没有定义哪种属性适用于表单控件,也没有定义如何使用CSS来给它们添加样式。用户代理可能会给这些属性应用CSS属性,建议开发者们把这种支持当做实验性质的,更高版本的CSS可能会进一步规范这个。

    BFC的特性:

    1)块级格式化上下文会阻止外边距叠加

    两个相邻的块框在同一个块级格式化上下文中时,它们之间垂直方向的外边距会发生叠加。换句话说,如果这两个相邻的块框不属于同一个块级格式化上下文,那么它们的外边距就不会叠加

    2)块级格式化上下文不会重叠浮动元素

    根据规定,一个块级格式化上下文的边框不能和它里面的元素的外边距重叠。这就意味着浏览器将会给块级格式化上下文创建隐式的外边距来阻止它和浮动元素的外边距叠加。由于这个原因,当给一个挨着浮动的块级格式化上下文添加负的外边距时将会不起作用(Webkit和IE6在这点上有一个问题——可以看这个测试用例)。 

    3)块级格式化上下文通常可以包含浮动

    详见: W3C CSS2.1 - 10.6.7 'Auto' heights for block formatting context roots 
      

    通俗地来说:创建了 BFC的元素就是一个独立的盒子,里面的子元素不会在布局上影响外面的元素,反之亦然,同时BFC任然属于文档中的普通流。

    至此,您或许明白了为什么 overflow:hidden或者auto可以闭合浮动了,真是因为父元素创建了新的BFC。对于张鑫旭在对《overflow与zoom”清除浮动”的一些认识 》一文中对于用包裹来解释闭合浮动的原理,我觉得是不够严谨的,而且没有依据。并且说道“Firefox等浏览器并没有haslayout的概念”,那么现代浏览器是有BFC的,从表现上来说,hasLayout 可以等同于 BFC。

  • 相关阅读:
    Cisco Packet Tracer 7.2
    "%Error opening tftp://255.255.255.255/network config"
    CPI 3.0磁盘空间不足!
    ASA Failover
    思科交换机配置单播MAC地址过滤
    WLC HA模式下的注意事项
    802.11r mixed mode
    IEEE 802.11r-2008
    iOS 上通过 802.11k、802.11r 和 802.11v 实现 Wi-Fi 网络漫游
    Flexconnect部署
  • 原文地址:https://www.cnblogs.com/youxin/p/2651592.html
Copyright © 2011-2022 走看看