css样式flo闲和庄怎么玩at浮动后,父元素塌陷解决方法
2025-06-21 00:11:57
7782
给父元素加CSS伪类clearfix:after。给父元素加宽高
2、如下例子:box父元素没有设置高度,
代码如下:
<!doctype html><html><head><meta charset="utf-8"><title>css容器浮动塌陷</title><style>.box{border:10px solid red;}.box1{width:30%;height:100px;background:blue; float:left;}.box2{width:70%;height:100px;background:#3FC3AC; float:left;}</style></head><body><div class="box"><div class="box1">box1</div><div class="box2">box2</div></div></body></html>
浏览器浏览如下:
height:0; /*将添加进去的内容高度设置为0,
当父元素里面的内容有浮动的时候,父元素会变成一条线。在这里我们可以称为“塌陷”。在这个对象被浏览器渲染后添加一定的内容*/
content:"."; /*content属性专门配合伪对象,*/
clear:both; /*将添加进去的内容作为清除浮动对象*/
}
添加clearfix:after后代码如下:
<!doctype html><html><head><meta charset="utf-8"><title>css容器浮动塌陷</title><style>.box{border:10px solid red;}.box1{width:30%;height:100px;background:blue; float:left;}.box2{width:70%;height:100px;background:#3FC3AC; float:left;}.clearfix:after{clear:both; display:block; content:"."; visibility:hidden;height:0;}.clearfix{zoom:1;}</style></head><body><div class="box clearfix"><div class="box1">box1</div><div class="box2">box2</div></div></body></html>
浏览器浏览如下:
解决以上容器浮动“塌陷”的方法:
1、
本文地址:http://gx2di.xny028cc.com/interested/6-876.html
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。