我们先看看浮动导致什么样的效果。

正常没有浮动的案例如下:

<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title>清除浮动的方法</title><style type="text/css">.container{background-color: lightblue;}           </style></head><body><div class="container"><h1>The farthest distance in the world</h1><p>The farthest distance in the world,Is the distance between fish and bird,One is in the sky, another is in the sea</p><p>世界上最遥远的距离,是鱼与飞鸟的距离,一个在天 一个却深潜海底</p></div></body>
</html>

没有浮动的效果图:

给h1和p添加浮动后的案例如下:

<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title>清除浮动的方法</title><style type="text/css">.container{background-color: lightblue;}   h1,p{float: left;}       </style></head><body><div class="container"><h1>The farthest distance in the world</h1><p>The farthest distance in the world,Is the distance between fish and bird,One is in the sky, another is in the sea</p><p>世界上最遥远的距离,是鱼与飞鸟的距离,一个在天 一个却深潜海底</p></div></body>
</html>

添加浮动后的效果图:

可以看到,对h1和p添加浮动之后,包裹它们的父元素背景颜色没有了,好像不能包裹一样。这就是浮动带来的麻烦,解决的方法有以下几种:

方法一:在包裹浮动元素的父元素中最后加入一个空标签

<div style="clear:both"></div>

代码如下:

<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title>清除浮动的方法</title><style type="text/css">.container{background-color: lightblue;}           h1,p{float: left;}</style></head><body><div class="container"><h1>The farthest distance in the world</h1><p>The farthest distance in the world,Is the distance between fish and bird,One is in the sky, another is in the sea</p><p>世界上最遥远的距离,是鱼与飞鸟的距离,一个在天 一个却深潜海底</p><div style="clear:both"></div></div></body>
</html>

效果图:

缺点:增加一个无意义的标签,违背结构和表现分离的web标准,如果后期维护中添加很多这样的标签会很不方便

方法二:在包裹浮动元素的父元素的css样式中加入overflow:auto;或者overflow:hidden;

代码如下:

<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title>清除浮动的方法</title><style type="text/css">.container{overflow: auto;background-color: lightblue;}           h1,p{float: left;}</style></head><body><div class="container"><h1>The farthest distance in the world</h1><p>The farthest distance in the world,Is the distance between fish and bird,One is in the sky, another is in the sea</p><p>世界上最遥远的距离,是鱼与飞鸟的距离,一个在天 一个却深潜海底</p></div></body>
</html>

效果图:

方法三:在包裹浮动元素的父元素的css样式中也设置相应的浮动样式

代码如下:

<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title>清除浮动的方法</title><style type="text/css">.container{float: left;background-color: lightblue;}           h1,p{float: left;}</style></head><body><div class="container"><h1>The farthest distance in the world</h1><p>The farthest distance in the world,Is the distance between fish and bird,One is in the sky, another is in the sea</p><p>世界上最遥远的距离,是鱼与飞鸟的距离,一个在天 一个却深潜海底</p></div></body>
</html>

效果图:

缺点:不可能让每一层都建立在一个浮动的层中,这会影响页面布局

方法四:在包裹浮动元素的父元素添加一个css样式clearfix。它的属性如下

.clearfix:before,
.clearfix:after{content:" ";display: table;
}.clearfix:after{clear:both;
}

代码如下:

<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title>清除浮动的方法</title><style type="text/css">.container{background-color: lightblue;}           h1,p{float: left;}.clearfix:before,.clearfix:after{content:" ";display: table;}.clearfix:after{clear:both;}</style></head><body><div class="container clearfix"><h1>The farthest distance in the world</h1><p>The farthest distance in the world,Is the distance between fish and bird,One is in the sky, another is in the sea</p><p>世界上最遥远的距离,是鱼与飞鸟的距离,一个在天 一个却深潜海底</p></div></body>
</html>

效果图:

最后一种方法比较常用。

CSS清除浮动的方法相关推荐

  1. div为空的时候 浮动没有效果_3种CSS清除浮动的方法

    点击上方 "前端技术精选" 关注,星标或者置顶 12点00分准时推送,第一时间送达 作者:html中文网 | 编辑:前端妹 来源:html.cn/web/css/19613.htm ...

  2. css为什么要用浮动_3种CSS清除浮动的方法

    点击上方 "前端技术精选" 关注,星标或者置顶 12点00分准时推送,第一时间送达 作者:html中文网 | 编辑:前端妹 来源:html.cn/web/css/19613.htm ...

  3. css清除浮动的方法及原因

    清除浮动的方法及原因 为什么要清除浮动 清除浮动的方法 方法1:为父元素设置高度 方法2:在受影响的父元素内部末尾添加块级元素-如div 方法3:伪元素清除法 方法4:给父元素设置overflow: ...

  4. web前端css清除浮动的方法总结

    方法1:祖先加高法 //不常用,不能适应页面的快速变化 如果一个元素要浮动,那么它的祖先元素一定要有高度.有高度的盒子,才能关住浮动. 只要浮动在一个有高度的盒子中,那么这个浮动就不会影响后面的浮动元 ...

  5. CSS 清除浮动的方法

    CSS中利用float属性可以使元素脱离文档流浮动到父元素的左侧或者浮动的父元素的右侧,这时如果对元素使用浮动属性,会导致父元素不能被撑开. CSS代码: .wrapper{width: 40em;b ...

  6. CSS清除浮动的方法及原理

    为什么清除浮动 1.浮动的缺点: 浮动虽然可以便于页面布局,但同时会产生一些问题,也就是我们常说的"副作用".一个元素设置了浮动(即 float 值为 left, right 或 ...

  7. 前端开发面试题—CSS清除浮动的方法

  8. 活学活用,CSS清除浮动的4种方法

    清除浮动这个问题,做前端的应该再熟悉不过了,咱是个新人,所以还是记个笔记,做个积累,努力学习向大神靠近. CSS清除浮动的方法网上一搜,大概有N多种,用过几种,说下个人感受. 1.结尾处加空div标签 ...

  9. css清除浮动无效,css清除浮动的处理方法

    根据<精彩绝伦的css> JS Bin .news{ outline:1px solid pink } .col{ float: left; width:33%; outline:1px ...

最新文章

  1. 三步解决C语言中struct字节对齐问题,Python进阶篇-struct字节对齐问题
  2. 导出Excle java
  3. linux命令:vmstat
  4. ImportError: No module named model_libs
  5. 使用mvc模式读取服务器上的文件,关于C#:如何使用asp.net MVC应用程序从服务器上的网络路径读取...
  6. 计算机软考网络管理员题,2020年计算机软考网络管理员考前测试题及答案
  7. 关于(int argc char **argv)
  8. 秋季唯美海报,打造的一系列秋季主题视觉
  9. C++_类和对象_对象特性_空指针访问成员函数_用法和问题解决---C++语言工作笔记050
  10. ssis 列转换_SSIS包中的行采样转换和百分比采样转换
  11. LARS算法---十折交叉验证
  12. android如何修改视频的分辨率,Android录制视频,视频压缩,设置分辨率,设置视频名称...
  13. 算法三:判断该年该月有几天
  14. mediapipe系列之一脸部特征点标记
  15. 2022-04-24 表单设计器动态插入脚本【低代码平台】
  16. R3LIVE调试记录
  17. nmap的下载与安装
  18. Java-c3p0原生写法
  19. 第164篇,陌生人和贵人(扶摇生财思维)
  20. Nginx证书配置:cer文件和jks文件转nginx证书.crt和key文件

热门文章

  1. 论文投稿指南——SCI选刊
  2. 防电脑辐射的方法,电脑防辐射技巧
  3. Spring环绕通知Around
  4. 看过这篇文章,一切关于NFT的疑问都可以迎刃而解
  5. PMP续证 | PDU具体操作图解
  6. 美团后台开发秋招面经汇总(更新至2021-08-13)
  7. 2022年3月 python一级 程序题 【买本子和画三角形】
  8. 12 行列式01--- 定义、计算 与性质: n级行列式的性质、行列式计算
  9. Python——模块:模块就是程序
  10. WPE教学之-截取操作篇