css清除浮动的几种方法

html-css013

css清除浮动的几种方法,第1张

1,父级div定义 height

<style type="text/css">

.div1{background:#000080border:1px solid red/*解决代码*/height:200px}

.div2{background:#800080border:1px solid redheight:100pxmargin-top:10px}

.left{float:leftwidth:20%height:200pxbackground:#DDD}

.rightright{float:rightrightwidth:30%height:80pxbackground:#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不一样时,会产生问题

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

评分:★★☆☆☆

2,结尾处加空div标签 clear:both

<style type="text/css">

.div1{background:#000080border:1px solid red}

.div2{background:#800080border:1px solid redheight:100pxmargin-top:10px}

.left{float:leftwidth:20%height:200pxbackground:#DDD}

.rightright{float:rightrightwidth:30%height:80pxbackground:#DDD}

/*清除浮动代码*/

.clearfloat{clear:both}

</style>

<div class="div1">

<div class="left">Left</div>

<div class="right">Right</div>

<div class="clearfloat"></div>

</div>

<div class="div2">

div2

</div>

3,父级div定义 伪类:after 和 zoom

<style type="text/css">

.div1{background:#000080border:1px solid red}

.div2{background:#800080border:1px solid redheight:100pxmargin-top:10px}

.left{float:leftwidth:20%height:200pxbackground:#DDD}

.rightright{float:rightrightwidth:30%height:80pxbackground:#DDD}

/*清除浮动代码*/

.clearfloat:after{display:blockclear:bothcontent:""visibility:hiddenheight:0}

.clearfloat{zoom:1}

</style>

<div class="div1 clearfloat">

<div class="left">Left</div>

<div class="right">Right</div>

</div>

<div class="div2">

div2

</div>

4,父级div定义 overflow:hidden

<style type="text/css">

.div1{background:#000080border:1px solid red/*解决代码*/width:98%overflow:hidden}

.div2{background:#800080border:1px solid redheight:100pxmargin-top:10pxwidth:98%}

.left{float:leftwidth:20%height:200pxbackground:#DDD}

.rightright{float:rightrightwidth:30%height:80pxbackground:#DDD}

</style>

<div class="div1">

<div class="left">Left</div>

<div class="right">Right</div>

</div>

<div class="div2">

div2

</div>

css浮动(float:none | left | right)在网页布局中我们经常使用的属性,也是经常会出现Bug的地方。

首先我们要搞清楚为什么要用到float呢?

网页布局中块级元素,在页面中独占一行,自上而下排列,也就是传说中的文档流。

可是我们要实现左右模块该怎么实现,这就需要用到float了,当我把第三个设置左浮动

然后我们在测试第四个也设置左浮动

下面我将第二个和第四个右浮动

我们在测试将第三个浮动取消

可是在做网页布局的时候不想浮动元素影响遮盖下一个元素该怎么解决?

这个时候就需要我们清除浮动(clear:both | left | right)

上面的例子 希望第三个左浮动,第二个和第四个右浮动,第五个不受影响在最下面。

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

清除浮动方法:

1、使用带clear属性的空元素

在浮动元素后使用一个空元素如<div class="clear"></div>,并在CSS中赋予.clear{clear:both}属性即可清理浮动。亦可使用<br class="clear" />或<hr class="clear" />来进行清理。

2、使用CSS的overflow属性

给浮动元素的容器添加overflow:hidden或overflow:auto可以清除浮动,另外在 IE6 中还需要触发 hasLayout ,例如为父元素设置容器宽高或设置 zoom:1。在添加overflow属性后,浮动元素又回到了容器层,把容器高度撑起,达到了清理浮动的效果。

3、给浮动的元素的容器添加浮动

给浮动元素的容器也添加上浮动属性即可清除内部浮动,但是这样会使其整体浮动,影响布局,不推荐使用。

4、使用邻接元素处理

什么都不做,给浮动元素后面的元素添加clear属性。

5、方法五:使用CSS的:after伪元素

给浮动元素的容器添加一个clearfix的class,然后给这个class添加一个:after伪元素实现元素末尾添加一个看不见的块元素(Block element)清理浮动。

需要注意的是为了IE6和IE7浏览器,要给clearfix这个class添加一条zoom:1触发haslayout。

总结

BFC清理浮动

通用的清理浮动法案

.clearfix{

*zoom:1

}

.clearfix:after{

content:"."

display:block

height:0

visibility:hidden

clear:left

}

.clearfix{

*zoom:1

}

.clearfix:after{

content:""

display:table

clear:both

}