3种CSS清除浮动的方法
(给前端大学加星标,提升前端技能.)
今天这篇文章给大家介绍3种CSS清除浮动的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。作者:html中文网
https://www.html.cn/web/css/19613.html
css代码:class="wrap">
class="float">浮动
class="clear">
class="nofloat">不想被浮动影响
现在虽然加了一个空的div,但是并没有给它清除浮动,所以目前的效果就是第三个子元素.nofloat还是收到浮动的影响。.wrap{
width:500px;
height:400px;
border:1px solid red;
margin:0 auto;
}
.float{
width:200px;
height:200px;
background:#ccc;
float:left;
}
.nofloat{
width:300px;
height:150px;
background:red;
}
刷新一下效果就出来了:.clear{
clear:both;
}
css代码:class="wrap">
class="float">浮动
class="nofloat">不想被浮动影响
效果是这样的:.wrap{
width:500px;
border:1px solid red;
margin:0 auto;
overflow:hidden;
}
.float{
width:200px;
height:200px;
background:#ccc;
float:left;
}
.nofloat{
width:300px;
height:150px;
background:red;
overflow:hidden;
}
css代码:class="wrap">
<div class="float">浮动div>
</div>
此时子元素浮动了,脱离了文档流,所以父元素高度酒塌陷了:.wrap{
width:500px;
border:1px solid red;
margin:0 auto;
}
.float{
width:200px;
height:200px;
background:#ccc;
float:left;
}
class="wrap clearfix">
<div class="float">浮动div>
</div>
现在刷新后的效果就是:.clearfix{
1; :
}
after{ :
content:'clear';
display:block;
height:0;
clear:both;
overflow:hidden;
visibility:hidden;
}
分享前端好文,点亮 在看
评论