您的位置:首页 > 其它

接上篇(解决float导致塌陷问题)之clear:both失效

2016-10-21 22:15 435 查看
在今天写的途中发现一个问题,虽然解决,可并不知道原因是什么,现在先记下来。

问题的简化版大概是这样:若父元素里有一个以上的元素为浮动,那么,清除浮动(无论是用伪类还是新增一个类的方法)无效。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.fu1{
border: 1px solid black;
zoom:1;
}
.son1{
height: 300px;
width: 300px;
float: left;
background-color: green;
}
.son2{
height: 300px;
width: 300px;
float: left;
background-color: yellow;
}
.fu2{
width: 800px;
height: 200px;
border: 2px solid black;
}
.clear:after{
content: ".";
height: 0;
visibility: hidden;
display: block;
clear: both;
}

</style>
</head>
<body>
<div class="fu1">
<div class="son1">
</div>
<div class="son2 .clear">
</div>
</div>
<div class="fu2">
</div>
</body>
</html>这样清除浮动仍然无法让父元素撑开。
只能通过父元素设置overflow:hidden 或者显式设置高度来撑开fu1高度

一定要注意,若设置父元素fu1 position:absolute时,父元素的高度是可以被撑开,并且体现了absolute的包裹性,表现即宽度也与内容自适应了,

但是fu1仍然不在文档流中!,所以fu2会跑去上面,达不到我们想要的效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: