本文主要和大家分享几种CSS清除浮动的方法,希望能帮助到大家。
1、父级p定义height
<style> .p1{background:#000080;border:1px solid red;height:200px;}
.left{float:left;width:20%;height:200px;background:#DDD}
.right{float:right;width:30%;height:80px;background:#DDD}</style><p> </p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13534" title="Lightning CSS 高性能 CSS 解析器 转换器 和 压缩工具"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20e352aa140.jpg" alt="Lightning CSS 高性能 CSS 解析器 转换器 和 压缩工具" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13534" title="Lightning CSS 高性能 CSS 解析器 转换器 和 压缩工具" class="overflowclass">Lightning CSS 高性能 CSS 解析器 转换器 和 压缩工具</a>
<p class="overflowclass">Lightning CSS 是由 Parcel 团队开发的基于 Rust 的 CSS 解析器、转换器、打包工具和压缩工具。此技能适用于需要优化样式表、为目标浏览器降低现代 CSS 语法以及将快速 CSS 处理集成到构建或重构工作流中的代理。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13534" title="Lightning CSS 高性能 CSS 解析器 转换器 和 压缩工具" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><p>Left</p> <p>Right</p>
父级p手动定义height,就解决了父级p无法自动获取高度的问题(只建议高度固定的布局使用)
2、结尾处加空p标签 clear:both
<style> .p1{background:#000080;border:1px solid red}
.left{float:left;width:20%;height:200px;background:#DDD}
.right{float:right;width:30%;height:80px;background:#DDD}
.clearfloat{clear:both}</style><p> </p><p>Left</p> <p>Right</p> <p></p>
添加一个空p,利用css提高的clear:both清除浮动,让父级p自动获取高度(以前使用的较多)
3、父级p定义伪类:after和 zoom
<style> .p1{background:#000080;border:1px solid red;}
.left{float:left;width:20%;height:200px;background:#DDD}
.right{float:right;width:30%;height:80px;background:#DDD}
.clearfloat:after{display:block;clear:both;content:"";visibility:hidden;height:0}
.clearfloat{zoom:1}</style><p> </p><p>Left</p> <p>Right</p>
IE8以上和非IE浏览器才支持:after,zoom(ie专有属性)可解决IE6、IE7浮动问题
4、父级p定义 overflow:hidden
<style> .p1{background:#000080;border:1px solid red;width:98%;overflow:hidden}
.left{float:left;width:20%;height:200px;background:#DDD}
.right{float:right;width:30%;height:80px;background:#DDD}</style><p> </p><p>Left</p> <p>Right</p>
必须定义width或zoom:1,同时不能定义height,使用overflow:hidden时,浏览器会自动检查浮动区域的高度
5、 父级定义: overflow:auto
<style> .p1{background:#000080;border:1px solid red;/*解决代码*/width:98%;overflow:auto}
.left{float:left;width:20%;height:200px;background:#DDD}
.right{float:right;width:30%;height:80px;background:#DDD}</style><p> </p><p>Left</p> <p>Right</p>
必须定义width或zoom:1,同时不能定义height,使用overflow:auto时,浏览器会自动检查浮动区域的高度
6、父级p也一起浮动
<style> .p1{background:#000080;border:1px solid red;width:98%;margin-bottom:10px;float:left}
.left{float:left;width:20%;height:200px;background:#DDD}
.right{float:right;width:30%;height:80px;background:#DDD}</style><p> </p><p>Left</p> <p>Right</p>
所有代码一起浮动,会产生新的浮动,仅作了解
7、父级p定义 display:table
<style> .p1{background:#000080;border:1px solid red;width:98%;display:table;margin-bottom:10px;}
.left{float:left;width:20%;height:200px;background:#DDD}
.right{float:right;width:30%;height:80px;background:#DDD}</style><p> </p><p>Left</p> <p>Right</p>
将p的属性变成表格
8、结尾处加 br标签 clear:both
<style> .p1{background:#000080;border:1px solid red;margin-bottom:10px;zoom:1}
.left{float:left;width:20%;height:200px;background:#DDD}
.right{float:right;width:30%;height:80px;background:#DDD}
.clearfloat{clear:both}</style><p> </p><p>Left</p> <p>Right</p> <br>
父级p定义zoom:1来解决IE浮动问题,结尾处加br标签clear:both
相关推荐:
前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!










