周五去听css样式的培训,讲到float导致div不能被撑开的问题,特记录如下:

在写HTML代码的时候,如果有一个DIV作为外部容器,内部的DIV如果设置了float样式,则外部的容器DIV因为内部没有clear,导致不能被撑开。

代码:

<html>
<body>

<div style="border:2px solid red;" class0="clearfix">
<div style="float:left;width:80px;height:80px;border:1px solid blue;">DIV1</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">DIV2</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">DIV3</div>
</div>

</body>
</html>

显示效果:

大家可以看到,作为外部容器的边框为红色的DIV,没有被撑开。

这是因为内部的DIV因为float:left之后,就丢失了clear:both和display:block的样式,所以外部的DIV不会被撑开。

我们想让外部容器的DIV随着内部DIV增多而增加高度,要怎么解决呢?

解决办法:

代码:

<html>
<body>

<style>
.clearfix:after {
content: " ";
display: block;
clear: both;
height: 0;
}
.clearfix {
zoom: 1;
}
</style>

<div style="border:2px solid red;" class="clearfix">
<div style="float:left;width:80px;height:80px;border:1px solid blue;">DIV1</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">DIV2</div>
<div style="float:left;width:80px;height:80px;border:1px solid blue;">DIV3</div>
</div>

</body>
</html>

显示效果:

最新文章

  1. C#的类及应用
  2. 多系统PE win UBUNTU OSX
  3. [LintCode] Longest Substring Without Repeating Characters
  4. python新技能get——看!源!码!
  5. finder怎么才能找到library
  6. 敏捷软件开发 原则 模式 与实践 - OCP原则
  7. phone number is not known @w@ have no phone, and thus no phone number
  8. 织梦dedecms调用栏目的SEO标题、描述、关键字的方法
  9. 几个简单的Makefile
  10. java Class的Long id初始化 为0的问题android数据库操做出现的 android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
  11. 安全通信 QSslSocket
  12. 边缘检测之Sobel检测算子
  13. 解决在IIS中调用Microsoft Office Excel组件后进程无法正常退出的问题
  14. jndi通俗理解以及它的指令缺陷
  15. 搭建一个MP-demo(mybatis_plus)
  16. Windows下Redis的安装和部署
  17. CLion之C++框架篇-优化开源框架,引入curl,实现get方式获取资源(四)
  18. (DFS)P1605 迷宫 洛谷
  19. Tensorflow一些常用基本概念与函数
  20. Nginx入门【转】

热门文章

  1. poj 3083 dfs,bfs
  2. python3里的Urllib库
  3. PHP提示Cannot modify header information - headers already sent by解决方法
  4. hdu2448 / 费用流 / harbin赛区c题
  5. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
  6. 洛谷P1352 没有上司的舞会
  7. springboot主要注解及其作用
  8. sql中Cast()函数的用法
  9. MySql常用函数积累
  10. ceph工作原理和安装