清除浮动的四种方法

  1. 加 clear: ...(见例1)
  2. 父级上增加属性 overflow:hidden(见例2.1)
  3. 在最后一个子元素的后面加一个空的 div,给它一个样式属性 clear: both(不推荐)(见例2.2)
  4. 使用成熟的清浮动样式类 clearfix(见例3)

少废话,上例子

例 1

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<link rel="stylesheet" type="text/css" href="./static/CSS/test.css">
</head>
<body>
<div class="box1">box1</div>
<div class="box2">box2</div>
<div class="box3">box3</div>
<div class="box4">box4</div>
<div class="box5">box5</div>
<div class="box6">box6</div>
</body>
</html>
div{
width: 100px;
height: 100px;
float: left;
}
.box1{
background: red;
}
.box2{
background: orange;
}
.box3{
background: yellow;
}
.box4{
background: green;
/* 清除浮动
left: 清除左浮动
right: 清除有浮动
both: 清除左右两边的浮动 */
/*clear: left; 只加上这句,效果见效果截图 2*/
/*clear: rightt; 只加上这句,显示上没有变化 */
}
.box5{
background: blue;
}
.box6{
background: indigo;
}
.box7{
background: purple;
}
  • 效果截图 1

  • 缩小浏览器的宽度后的截图

  • 效果截图 2

例 2

<!-- 例2 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<link rel="stylesheet" type="text/css" href="./static/CSS/test.css">
</head>
<body>
<div class="wrap">
<div class="box1">box1</div>
<div class="box2">box2</div>
<div class="box3">box3</div>
<div class="box4">box4</div>
<div class="box5">box5</div>
<div class="box6">box6</div>
<div class="box7">box7</div>
</div>
</body>
</html>
<!-- 例2.1 -->
.wrap{
border: 2px solid;
/* 清除浮动
解决父级元素高度无法撑开问题
注意: 是给浮动元素的父级添加 */
/*overflow: hidden; 加上这句,见效果截图 4 */
}
.box1, .box2, .box3, .box4, .box5, .box6, .box7{
width: 100px;
height: 100px;
float: left;
}
.box1{
background: red;
}
.box2{
background: orange;
}
.box3{
background: yellow;
}
.box4{
background: green;
clear: left;
}
.box5{
background: blue;
}
.box6{
background: indigo;
}
.box7{
background: purple;
}
  • 效果截图 3

  • 效果截图 4

<!-- 例2.2 html 不变 -->
.wrap{
border: 2px solid;
}
.wrap:after{ /* 伪类选择器 */
/* 也有 before,但一般使用 after
这种方法的思路:
1. 在父级元素后插入一个空的字符串
2. 将这个字符串转成块级元素
3. 用 clear: both 给此元素清除浮动
4. 没有添加不必要的标签,不影响页面结构
注意:给浮动元素的父级添加 */
content: '';
display: table;
/* display: block; 从效果上看,block 与 table 一致 */
clear: both;
}
.box1, .box2, .box3, .box4, .box5, .box6, .box7{
width: 100px;
height: 100px;
float: left;
}
.box1{
background: red;
}
.box2{
background: orange;
}
.box3{
background: yellow;
}
.box4{
background: green;
clear: left;
}
.box5{
background: blue;
}
.box6{
background: indigo;
}
.box7{
background: purple;
}
  • 效果截图 5

    • 与效果截图 4 一般无二,故略

例 3

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<link rel="stylesheet" type="text/css" href="./static/CSS/test.css">
</head>
<body>
<div class="wrap">
<div class="box1">box1</div>
<div class="box2">box2</div>
<div class="box3">box3</div>
<div class="box4">box4</div>
<div class="box5">box5</div>
<div class="box6">box6</div>
<div class="box7">box7</div>
<div class="cl"></div> <!-- 多了这句 -->
</div>
</body>
</html>
.wrap{
border: 2px solid;
}
.cl{ /* 可行,但不推荐,因为会对页面结构产生影响 */
clear: both;
}
.box1, .box2, .box3, .box4, .box5, .box6, .box7{
width: 100px;
height: 100px;
float: left;
}
.box1{
background: red;
}
.box2{
background: orange;
}
.box3{
background: yellow;
}
.box4{
background: green;
clear: left;
}
.box5{
background: blue;
}
.box6{
background: indigo;
}
.box7{
background: purple;
}
  • 效果截图 6

    • 与效果截图 4 一般无二,故略

补充

  • 有时会加一句 zoom:1;,这样做是为了兼容 IE

参考:北京图灵学院的 Web 前端公开课

最新文章

  1. 如何写出安全的API接口?接口参数加密签名设计思路
  2. 挖一挖C#中那些我们不常用的东西之系列(5)——FlagAttribute
  3. sqlite数据类型
  4. PHP截断函数mb_substr()详细介绍
  5. WebBrowser 中遍历所有的frames
  6. BEA-150021 - The admin server failed to authenticate the identity of the user username starting the managed server.
  7. hdu 4258 Covered Walkway
  8. ESP8266 TCP传输AT指令顺序
  9. 利用jquery的imgAreaSelect插件实现图片裁剪示例
  10. hello,world不使用ARC
  11. Android 2.3.5源码 更新至android 4.4,能够下载,度娘网盘
  12. 20165304《JAVA程序设计》第二周学习总结
  13. Docker学习笔记1:CentOS7 下安装Docker
  14. The 2nd tip of DB Query Analyzer
  15. [Linux]返回被阻塞的信号集
  16. JS将/Date(1446704778000)/转换成str
  17. 腾讯技术分享:微信小程序音视频与WebRTC互通的技术思路和实践
  18. Ajax传递json数据简介和一个需要注意的小问题
  19. 利用ngnix解决跨域问题
  20. wpf 加阴影效果导致内容模糊的问题解决

热门文章

  1. Batchsize与learning rate
  2. web框架-(一)初识web框架
  3. u-boot-2019.07 移植步骤
  4. luoguP1445 [Violet]樱花
  5. Git回滚到指定的commit
  6. windows 10安装python3和python2
  7. Apache主要配置文件http.conf
  8. 转载--C++的反思
  9. 双系统使用Linux引导
  10. uploadify加ASP.NET MVC3.0上传文件(可多条)