[CSS]清除浮动的三个方法
来源:互联网 发布:推倒女神的体验知乎 编辑:程序博客网 时间:2024/05/17 20:11
一个没有设置高度的容器div内如果存在浮动元素(即使用了属性float:left或者float:right),那么该父级元素会无法展开。
举个例子,有一个div容器,div容器里有两个小容器,分别向左和向右浮动,为了区别这三个容器,分别设置了不同颜色的边框。
<html><head><style>.content {width:500px;border:1px solid red;}.left {width:100px;height:100px;border:1px dashed blue;float:left;}.right {width:100px;height:100px;border:1px dashed green;float:right;}</style></head><body><div class="content"><div class="left"></div><div class="right"></div></div></body></html>
产生效果:
注意到父级元素content没有展开。
为了使父级元素展开,有三种方法:
第一:设置父级元素的高度,但是要事先知道内容的高度,这里内部的元素高度是100像素,加上上下边框高度2像素,一共是102像素。
<html><head><style>.content {width:500px;border:1px solid red;height:102px;}.left {width:100px;height:100px;border:1px dashed blue;float:left;}.right {width:100px;height:100px;border:1px dashed green;float:right;}</style></head><body><div class="content"><div class="left"></div><div class="right"></div></div></body></html>
第二:添加样式,并在父级元素结束标签前添加both:clear样式。
<html><head><style>.content {width:500px;border:1px solid red;}.left {width:100px;height:100px;border:1px dashed blue;float:left;}.right {width:100px;height:100px;border:1px dashed green;float:right;}.clear {clear:both;}</style></head><body><div class="content"><div class="left"></div><div class="right"></div><div class="clear"></div></div></body></html>
第三:添加overflow属性:
<html><head><style>.content {width:500px;border:1px solid red;overflow:hidden;}.left {width:100px;height:100px;border:1px dashed blue;float:left;}.right {width:100px;height:100px;border:1px dashed green;float:right;}</style></head><body><div class="content"><div class="left"></div><div class="right"></div></div></body></html>
- CSS清除浮动的三个方法
- [CSS]清除浮动的三个方法
- css清除浮动使父级元素展开的三个方法
- css 清除浮动的方法
- CSS清除浮动的方法
- CSS清除浮动的方法
- CSS 清除浮动的方法
- 清除css浮动的方法
- CSS清除浮动的方法
- css清除浮动的方法
- css清除浮动的方法
- CSS清除浮动的方法
- CSS清除浮动的方法
- css清除浮动的方法
- 为什么清除浮动?清除css浮动的三种方法
- CSS清除浮动方法
- CSS清除浮动方法
- CSS:清除浮动的最优方法
- Android WebView总结
- python登录baidu机器人
- thrift-异步client&server使用例子
- 对php代码优化的一些解释
- 制造企业3.0 基于流程的新的信息管理范例
- [CSS]清除浮动的三个方法
- 数据结构基础_高仿remove(value)方法
- A^B%C 快速幂模版
- HDU 3584 三维树状数组
- 监听器
- 个人笔记
- Postgres 9.2 Replication Configuration on CentOS 5
- tomcat设置上下文和配置多个应用
- Valid Palindrome