zoukankan      html  css  js  c++  java
  • CSS 清除浮动的4种方法

    此为未清除浮动源代码,运行代码无法查看到父级元素浅黄色背景。<style type=”text/css”>

    1. <!–
    2. *{margin:0;padding:0;}
    3. body{font:36px bold; color:#F00; text-align:center;}
    4. #layout{background:#FF9;}
    5. #left{float:left;20%;height:200px;background:#DDD;line-height:200px;}
    6. #right{float:right;30%;height:80px;background:#DDD;line-height:80px;}
    7. –>
    8. </style>
    9. <div id=”layout”>
    10. <div id=”left”>Left</div>
    11. <div id=”right”>Right</div>
    12. </div>
    复制代码

    未清除浮动前如图所示:

    1.png

    四种清除浮动方法如下:


    1、使用空标签清除浮动。 我用了很久的一种方法,空标签可以是div标签,也可以是P标签。这种方式是在需要清除浮动的父级元素内部的所有浮动元素后添加这样一个标签清除浮动,并为其定义CSS代码:clear:both。此方法的弊端在于增加了无意义的结构元素。
    对于使用额外标签清除浮动(闭合浮动元素),是W3C推荐的做法。至于使用<br />元素还是空<div></div>可以根据自己的喜好来选(当然你也可以使用其它块级元素)。不过要注意的 是,<br />本身是有表现的,它会多出一个换行出来,所以要设定它的heigh为0,以隐藏它的表现。所以大多数情况下使用空<div>比较合 适。<style type=”text/css”>

    1. <!–
    2. *{margin:0;padding:0;}
    3. body{font:36px bold; color:#F00; text-align:center;}
    4. #layout{background:#FF9;}
    5. #left{float:left;20%;height:200px;background:#DDD;line-height:200px;}
    6. #right{float:right;30%;height:80px;background:#DDD;line-height:80px;}
    7. .clear{clear:both;}
    8. –>
    9. </style>
    10. <div id=”layout”>
    11. <div id=”left”>Left</div>
    12. <div id=”right”>Right</div>
    13. <div class=”clear”>
    14. </div>
    15. </div>
    复制代码

    2、 使用overflow属性。 此方法有效地解决了通过空标签元素清除浮动而不得不增加无意代码的弊端。使用该方法是只需在需要清除浮动的元素中定义CSS属 性:overflow:auto,即可!也可以用overflow:hidden;”zoom:1″用于兼容IE6,也可以用100%。
    不过使用overflow的时候,可能会对页面表现带来影响,而且这种影响是不确定的,你最好是能在多个浏览器上测试你的页面;

    1. <style type=”text/css”>
    2. <!–
    3. *{margin:0;padding:0;}
    4. body{font:36px bold; color:#F00; text-align:center;}
    5. #layout{background:#FF9;overflow:auto;zoom:1; }   /* overflow:auto可以换成overflow:hidden,zoom:1可以换成100%*/
    6. #left{float:left;20%;height:200px;background:#DDD;line-height:200px;}
    7. #right{float:right;30%;height:80px;background:#DDD;line-height:80px;}
    8. –>
    9. </style>
    10. <div id=”layout”>
    11. <div id=”left”>Left</div>
    12. <div id=”right”>Right</div>
    13. </div>
    复制代码

    3、 使用after伪对象清除浮动。 该方法只适用于非IE浏览器 。具体写法可参照以下示例。使用中需注意以下几点。一、该方法中必须为需要清除浮动元素的伪对象中设置height:0,否则该元素会比实际高出若干像 素;二、content属性是必须的,但其值可以为空,蓝色理想讨论该方法的时候content属性的值设为”.”,但我发现为空亦是可以 的。<style

    1. type=”text/css”>
    2. <!–
    3. *{margin:0;padding:0;}
    4. body{font:36px bold; color:#F00; text-align:center;}
    5. #layout{background:#FF9;}
    6. #layout:after{display:block;clear:both;content:”";visibility:hidden;height:0;}
    7. #left{float:left;20%;height:200px;background:#DDD;line-height:200px;}
    8. #right{float:right;30%;height:80px;background:#DDD;line-height:80px;}
    9. –>
    10. </style>
    11. <div id=”layout”>
    12. <div id=”left”>Left</div>
    13. <div id=”right”>Right</div>
    14. </div>
    复制代码

    4、浮动外部元素,float-in-float。这种方法很简单,就是把“#outer”元素也进行浮动(向左或者向右)。
    但是这种方法带来的别外一个问题就是和“#outer”相邻的下一个元素会受到“#outer”的影响位置会产生变化,所以使用这种方法一定要小心。有选 择把页面中的所有元素都浮动起来,最后使用一个适当的有意义的元素(比如页脚)进行清理浮动,这有助于减少不必要的标记,但是过多的浮动会增加布局的难 度。<style type=”text/css”>

    1. <!–
    2. *{margin:0;padding:0;}
    3. body{font:36px bold; color:#F00; text-align:center;}
    4. #layout{background:#FF9;float:left;}
    5. #left{float:left;20%;height:200px;background:#DDD;line-height:200px;}
    6. #right{float:right;30%;height:80px;background:#DDD;line-height:80px;}
    7. –>
    8. </style>
    9. <div id=”layout”>
    10. <div id=”left”>Left</div>
    11. <div id=”right”>Right</div>
    12. </div>
    复制代码

    前三种方法清除浮动后如图所示:(第四种方法将导致外层div不会占整行,因为自身float:left了)

    2.png

  • 相关阅读:
    Emulator PANIC: Could not open: AVD2.3.1
    VC++ 6.0 快捷键
    eclipse 中文版 变成 英文版 方法
    SharedPreferences 用法
    subString
    Android键盘属性
    【Android异常】The specified child already has a parent. You must call removeView() on the child's parent first.
    ListView的ScrollListener
    Android 自定义格式的对话框
    Android ListView 设置
  • 原文地址:https://www.cnblogs.com/theWayToAce/p/5558755.html
Copyright © 2011-2022 走看看