当咱们用一个div(就叫大盒子吧) 取包裹其余盒子(小盒子)的时候,小盒子所有设置了浮动float, 从咱们平常认知当中:小盒子从大盒子中拿出来,那么大盒子知识内容部分(content)空了并不影响到什么. 可是在这里会形成高度塌陷, 父元素的高度会消失布局
.parent{ background-color: #ccc; } .box01{ width:100px; height:50px; background-color:pink; float:left; } .box02{ width:200px; height:100px; background-color:lightblue; float:left; } .box03{ width:300px; height:150px; background-color:yellow; float:left; }
<div class="parent"> <div class="box01"></div> <div class="box02"></div> <div class="box03"></div> </div>
浮动前
浮动后
上面两组图是悬浮先后的对比,悬浮后父元素(大盒子)因为没有内容的支撑,形成了高度塌陷(height:0;)spa
伪元素:after用的最多的地方就是清除浮动,这样不会给结构上添加负担,并且不会影响布局
例如code
.parents::after{ content:""; /*注意:这里必定要写 即便没有要写的内容也要有.*/ clear:both; /*清除两边的元素*/ display:block;/*伪元素行内的特性*/ }
后面还会持续更新...blog