码迷,mamicode.com
首页 > Web开发 > 详细

css清除浮动的几种方法整理

时间:2014-06-18 13:22:15      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   ext   color   

此为未清除浮动源代码,运行代码无法查看到父级元素浅黄色背景。

<style type="text/css">
<!*{margin:0;padding:0;}
body{font:36px bold; color:#F00; text-align:center;}
#layout{background:#FF9;}
#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>
<div id="layout">
<div id="left">Left</div>
<div id="right">Right</div>
</div>

未清除浮动前如图所示:

四种清除浮动方法如下:

1、使用空标签清除浮动。

我用了很久的一种方法,空标签可以是div标签,也可以是P标签。

这种方式是在需要清除浮动的父级元素内部的所有浮动元素后添加这样一个标签清除浮动,并为其定义CSS代码:clear:both。

此方法的弊端在于增加了无意义的结构元素。

对于使用额外标签清除浮动(闭合浮动元素),是W3C推荐的做法。

至于使用<br />元素还是空<div></div>可以根据自己的喜好来选(当然你也可以使用其它块级元素)。

不过要注意的是,<br />本身是有表现的,它会多出一个换行出来,所以要设定它的heigh为0,以隐藏它的表现。所以大多数情况下使用空<div>比较合适。

    <style type="text/css">
    <!*{margin:0;padding:0;}
    body{font:36px bold; color:#F00; text-align:center;}
    #layout{background:#FF9;}
    #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>
    <div id="layout">
    <div id="left">Left</div>
    <div id="right">Right</div>
    <div class="clear">
    </div>
    </div>

 

2、使用overflow属性

此方法有效地解决了通过空标签元素清除浮动而不得不增加无意代码的弊端。

使用该方法是只需在需要清除浮动的元素中定义CSS属性:

overflow:auto,即可!也可以用overflow:hidden;”zoom:1″用于兼容IE6,也可以用width:100%。

不过使用overflow的时候,可能会对页面表现带来影响,而且这种影响是不确定的,你最好是能在多个浏览器上测试你的页面;

<style type="text/css">
<!*{margin:0;padding:0;}
body{font:36px bold; color:#F00; text-align:center;}
#layout{background:#FF9;overflow:auto;zoom:1; }   /* overflow:auto可以换成overflow:hidden,zoom:1可以换成width:100%*/
#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>
<div id="layout">
<div id="left">Left</div>
<div id="right">Right</div>
</div>

 

3、使用after伪对象万能清除浮动。具体写法可参照以下示例。


<style type="text/css">
<!*{margin:0;padding:0;}
body{font:36px bold; color:#F00; text-align:center;}
#layout{background:#FF9;}
#layout{*zoom:1}
#layout:after{content:\20;display:block;height:0;clear:both}
#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>
<div id="layout">
<div id="left">Left</div>
<div id="right">Right</div>
</div>

4、浮动外部元素,float-in-float。这种方法很简单,就是把“#outer”元素也进行浮动(向左或者向右)。

但是这种方法带来的别外一个问题就是和“#outer”相邻的下一个元素会受到“#outer”的影响位置会产生变化,所以使用这种方法一定要小心。

选择把页面中的所有元素都浮动起来,最后使用一个适当的有意义的元素(比如页脚)进行清理浮动,这有助于减少不必要的标记,但是过多的浮动会增加布局的难度。


<style type="text/css">
<!*{margin:0;padding:0;}
body{font:36px bold; color:#F00; text-align:center;}
#layout{background:#FF9;float:left;}
#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>
<div id="layout">
<div id="left">Left</div>
<div id="right">Right</div>
</div>

css清除浮动的几种方法整理,布布扣,bubuko.com

css清除浮动的几种方法整理

标签:style   class   blog   code   ext   color   

原文地址:http://www.cnblogs.com/aobingyan/p/3791885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!