<style>
#main{
width: 500px;
height: 300px;
background-color: aquamarine;
}
#child1{
width: 100px;
height: 100px;
background-color: blueviolet;
float: left;
}
#child2{
width: 100px;
height: 100px;
background-color: rgb(59, 145, 236);
}
</style>
<body>
<div id="main">
<div id="child1"></div>
<div id="child2"></div>
</div>
</body>
没浮动之前是这样的
child1浮动到左边, 由于浮动造成脱离文档流, 脱离文档流造成空间释放,这样就导致child2跑到child1的位置, child1覆盖了child2
子元素浮动造成子元素脱离文档流,空间释放后,导致p标签跑到已经脱离文档流的盒子上去
用clear:both
<style>
#main{
width: 500px;
height:
300px;
background-color: aquamarine;
}
#child1{
width: 100px;
height: 100px;
background-color: rgb(200, 154, 242);
float: left;
}
#child2{
width: 100px;
height: 100px;
background-color: rgb(59, 145, 236);
float: left;
}
p{
background-color: rgb(176, 176, 240);
clear: both;
}
</style>
<body>
<div id="main">
<div id="child1"></div>
<div id="child2"></div>
<p>乒乒乓乓</p>
</div>
</body>