CSS 浮动清理,不使用 clear:both标签
2008-07-23 00:00
621 查看
例如:
<div style="background:#666;"> <!-- float container -->
<div style="float:left; width:30%; height:40px;background:#EEE; ">Some Content</div>
</div>
此时预览此代码,我们会发现最外层的父元素float container,并没有显示。这是因为子元素因进行了浮动,而脱离了文档流,导致父元素的height为零。
若将代码修改为:
<div style="background:#666;"> <!-- float container -->
<div style="float:left; width:30%; height:40px;background:#EEE; ">Some Content</div>
<div style="clear:both"></div>
</div>
注意,多了一段清理浮动的代码。这是一种好的CSS代码习惯,但是这种方法增加了无用的元素。这里有一种更好的方法,将HTML代码修改为:
<div class="clearfix" style="background:#666;"> <!-- float container -->
<div style="float:left; width:30%; height:40px;background:#EEE; ">Some Content</div>
</div>
定义CSS类,进行“浮动清理”的控制:
此时,预览以上代码( 删去这种注释 ),会发现即使子元素进行了浮动,父元素float container仍然会将其包围,进行高度自适应。
<div style="background:#666;"> <!-- float container -->
<div style="float:left; width:30%; height:40px;background:#EEE; ">Some Content</div>
</div>
此时预览此代码,我们会发现最外层的父元素float container,并没有显示。这是因为子元素因进行了浮动,而脱离了文档流,导致父元素的height为零。
若将代码修改为:
<div style="background:#666;"> <!-- float container -->
<div style="float:left; width:30%; height:40px;background:#EEE; ">Some Content</div>
<div style="clear:both"></div>
</div>
注意,多了一段清理浮动的代码。这是一种好的CSS代码习惯,但是这种方法增加了无用的元素。这里有一种更好的方法,将HTML代码修改为:
<div class="clearfix" style="background:#666;"> <!-- float container -->
<div style="float:left; width:30%; height:40px;background:#EEE; ">Some Content</div>
</div>
定义CSS类,进行“浮动清理”的控制:
.clearfix:after {}{ content: "."; clear: both; height: 0; visibility: hidden; display: block; } /* 这是对Firefox进行的处理,因为Firefox支持生成元素,而IE所有版本都不支持生成元素 */ .clearfix {}{ display: inline-block; } /* 这是对 Mac 上的IE浏览器进行的处理 */ /**//* Hides from IE-mac \*/ * html .clearfix {}{height: 1%;} /* 这是对 win 上的IE浏览器进行的处理 */ .clearfix {}{display: block;} /* 这是对display: inline-block;进行的修改,重置为区块元素*/ /**//* End hide from IE-mac */
此时,预览以上代码( 删去这种注释 ),会发现即使子元素进行了浮动,父元素float container仍然会将其包围,进行高度自适应。
相关文章推荐
- CSS:浮动清理,不使用 clear:both标签 (转)
- CSS 浮动清理,不使用 clear:both标签
- CSS:浮动清理,不使用 clear:both标签
- 更简洁的 CSS 清理浮动方式(使用 :after 伪类来提供浮动块后的 clear:both)
- css基础 使用div块清除无序列表ul,li中的左右浮动的简单示例 clear:both
- CSS中关于浮动(float)和清理(clear)的使用总结
- css基础 使用div块清除无序列表ul,li中的左右浮动的简单示例 clear:both
- css基础 clear属性:both 使用div块清除左右浮动的简单示例
- css基础 clear属性:both 使用div块清除左右浮动的简单示例
- CSS clear both清除浮动
- CSS清除浮动常用方法小结 CSS clear both {overflow:auto;zoom:1;}
- 2016/2/24 1,css有几种引入方式 2,div除了可以声明id来控制,还可以声明什么控制? 3,如何让2个div,并排显示。4,清除浮动 clear:left / right / both
- css之使用clearfix类清除浮动
- CSS clear both清除浮动
- DIV+CSS clear both清除产生浮动
- CSS clear both清除浮动
- CSS清除浮动常用方法小结 CSS clear both {overflow:auto;zoom:1;}
- CSS clear both清除浮动总结
- CSS 浮动(float:left),清除浮动(clear:both)