大多数前端使用.clearfix:after{ .....}  和 .clearit{....}的组合来清除浮动。

  前端开发经常用到浮动 float:left; float:right; 有浮动就需要清除他们,after伪类由于受到ie6 7的不支持所以大多数时候,一般都需要定义一个固定的class名设置属性clear的值both的div 两者配合使用.

<style type="text/css">
*{ margin:; padding:}
.left{ float: left;}
.clearfix:after { content:"."; display:block; height:; visibility:hidden; clear:both; } //伪类清除
.clearfix { zoom:; }
.clearit { clear:both; height:; font-size:; overflow:hidden; } //设置class名清除
.main{ width: 1000px;}
.myRight,.myLeft{ width: 200px; height: 200px; background: #ddd;}
.myRight{ background: red}
</style>

结构一:

<div class="main">
<div class="myLeft left">左侧</div>
<div class="myRight left">右侧</div>
</div>
<div class="footer">
<p>底部</p>
</div>

1  此时不清除浮动 标准浏览器 和ie8+  p标签会跑到 右侧浮动div的旁边 如图:

IE6 IE7下

我们可以看出,标准和ie8下 class名为main的div 没被撑开,IE6 IE7下却被浮动元素撑开了。

我们只需要解决 标准 和 IE8+的浮动 就可以;现在我们给main 追加个class名如下:

<div class="main clearfix">    // 此处追加
<div class="myLeft left">左侧</div>
<div class="myRight left">右侧</div>
</div>
<div class="footer">
<p>我是底部</p>
</div>

因为我们在css中设置了 .clearfix:after{..}所以浮动就不会继承下去

此时给 .main加上背景{ background:blue };

我们发现  标准和 IE6+   的 main 都已被撑开如下图:

IE6下

标准下:

其他浏览器下就不截图了。

结构二:

此时需要用到  .clearit{ ...}

<style type="text/css">

*{ margin: 0; padding: 0}

.left{ float: left;}

.clearfix:after { content:"."; display:block; height:0; visibility:hidden; clear:both; }                                                             //伪类清除

.clearfix { zoom:1; }

.clearit { clear:both; height:0; font-size:0; overflow:hidden; }  //设置class名清除

.main{ width: 1000px;}

.myLast,.myRight,.myLeft{ width: 200px; height: 200px; background: #ddd;}

.myRight{ background: red}

.myLast{ background:purple; height:140px}

</style>

////////此时浮动 元素 和 不需要浮动的元素 被包在同一个 父亲下

<div class="main">

<div class="myLeft left">左侧</div>

<div class="myRight left">右侧</div>

<div class="myLast">最后一个</div>

</div>

IE6 下

元素继承浮动跑到 浮动元素旁边 而且 有自己的背景

IE8+ 和标准

最后一个DIV没继承浮动 但是 元素内的 内容 被挡在浮动DIV后面,背景消失,钻入浮动元素底下。

此时 给右侧DIV加clearfix  class名;如下图:

<div class="main">

<div class="myLeft left">左侧</div>

<div class="myRight left clearfix">右侧</div>

<div class="myLast">最后一个</div>

</div>

标准下和ie6+ 下 效果不变;

<div class="main">

<div class="myLeft left">左侧</div>

<div class="myRight left">右侧</div>

<div class="clearit"></div>   //换种方法加上class为clearit的div

<div class="myLast">最后一个</div>

</div>

ie6+ 和标准下 如下图:

此时 ie6+和标准下 显示效果一致  浮动被清除;

这种简单结构的时候 也可给 最后一个div 设置 clear:both 这个属性和值;也能得到此效果,

在结构比较复杂 清除频繁的时候 不如 在浮动元素后面 加一个 <div class="clearit"></div>更方便,    当然 能用after伪类清除浮动的时候尽量用 伪类清除,这要 既减少冗余代码,用能便于以后修改和维护!!!

通过指定CSS属性float的值,从而使元素向左或向右浮动,然后由后继元素向上移动以填补前面元素的浮动而空出的可用空间。CSS的float属性,作用就是改变块元素对象的默认显示方式,HTML标签设置了float属性之后,它将不再独自占据一行,从而可以实现多个元素同处一行的效果。Float的功能很强大,但是如果没有好好掌握而使用很可能会成为你调试样式的噩梦。

使用Float样式,如果没有清除浮动,那么有浮动元素的父元素容器将无法自动撑开。如果没有清除内部浮动,此时会导致浮动的父元素无法自动撑开到本身应有的高度。也就是说:当一个元素是浮动的,如果没有关闭浮动时,其父元素不会包含这个浮动元素,因为此时浮动元素从文档流中脱离。

所以需要在样式定义的后面进行清除浮动,清除浮动的方法有几种:

Clear:both清除浮动

clear清除浮动主要是借用clear属性来清除浮动,这是一种比较陈旧的清除浮动方法,但是感觉一般遇到这种问题都会用这种方法去清除浮动。使用clear:both来清除浮动,我们需要在需要清除浮动地方的后面紧接着增加一个额外元素,比如说一个div标签,并且定义他们的样式为“clear:both”,其通常使用的结构方式如下:

复制代码

代码如下:

<div class="demo A">
<div class="demoB demoFloat">float left</div>
<div class="demoC demoFloat">float right</div>
<div class="demoD demoFloat">not float</div>
<div class="clear"></div>
</div>
复制代码

代码如下:

<pre name="code" class="css"> .clear {
clear:both;/*主要使用这个属性来清除浮动*/
/*为了不让ie具有一定的空间,个人建议加上下面三个属性*/
height: 0;
line-height: 0;
font-size: 0;
}</pre>
<pre></pre>
<p></p>
<pre></pre>
<p></p>
<h4 style="margin:0px; line-height:30px; color:rgb(81,177,72); font-family:'Microsoft Yahei'"><a name="t1"></a>
<span style="white-space:pre"></span>2.使用overflow</h4>
<p style="margin-top:0px; margin-bottom:9px; color:rgb(64,64,64); font-family:'Microsoft Yahei'; line-height:28px">
<span style="white-space:pre"></span>用overflow方法来清除浮动相对来说比较简单,只需要在有浮动的元素上面加上下面的属性:</p>
<p style="margin-top:0px; margin-bottom:9px; color:rgb(64,64,64); font-family:'Microsoft Yahei'; line-height:28px">
</p><pre name="code" class="css"> .A {
overflow: auto;
zoom: 1;/*在IE下触发其layout,也要以使用_height:1%来代替zoom*/
}</pre><p></p>
<p style="margin-top:0px; margin-bottom:9px; color:rgb(64,64,64); font-family:'Microsoft Yahei'; line-height:28px">
<span style="white-space:pre"></span>使用overflow属性来清除浮动有一点需要注意,overflow属性共有三个属性值:hidden,auto,visible。我们可以使用hiddent和auto值来清除浮动,但切记不能使用visible值,如果使用这个值将无法达到清除浮动效果,其他两个值都可以。</p>
<p style="margin-top:0px; margin-bottom:9px; color:rgb(64,64,64); font-family:'Microsoft Yahei'; line-height:28px">
<span style="white-space:pre"></span>对于overflow属性清滁浮动我们还可以这样应用:</p>
<p style="margin-top:0px; margin-bottom:9px; color:rgb(64,64,64); font-family:'Microsoft Yahei'; line-height:28px">
</p><pre name="code" class="css"> .A {
overflow: auto;/*除IE6以及其以下版本不识别之外,其他浏览器都识别*/
}
* html .A {
height: 1%; /* IE5-6 */
}</pre><p></p>
<h4 style="margin:0px; line-height:30px; color:rgb(81,177,72); font-family:'Microsoft Yahei'"><a name="t2"></a>
<span style="white-space:pre"></span>3.clearfix方法</h4>
<p style="margin-top:0px; margin-bottom:9px; color:rgb(64,64,64); font-family:'Microsoft Yahei'; line-height:28px">
<span style="white-space:pre"></span>这种方法清除浮动是现在网上最拉风的一种清除浮动,是利用:after和:before来在元素内部插入两个元素块,从而达到清除浮动的效果。其实现原理类似于clear:both方法,只是区别在于:clear在html插入一个div.clear标签,而clearfix利用其伪类clear:fix在元素内部增加一个类似于div.clear的效果。下面来看看其具体的使用方法:</p>
<p style="margin-top:0px; margin-bottom:9px; color:rgb(64,64,64); font-family:'Microsoft Yahei'; line-height:28px">
HTML Code:</p>
<p style="margin-top:0px; margin-bottom:9px; color:rgb(64,64,64); font-family:'Microsoft Yahei'; line-height:28px">
</p><pre name="code" class="css"> <div class="demo A clearfix">
<div class="demoB demoFloat">float left</div>
<div class="demoC demoFloat">float right</div>
<div class="demoD demoFloat">not float</div>
</div></pre><p></p>
<p style="margin-top:0px; margin-bottom:9px; color:rgb(64,64,64); font-family:'Microsoft Yahei'; line-height:28px">
<span style="white-space:pre"></span>使用clearfx来清除浮动最主要掌握一点,需要在有浮动元素的父元素中加入一个叫clearfix的class名称,比如说我们这个例子,我们需要在div.A中加入一个clearfix的class名。接着在给这个clearfix加上样式</p>
<p style="margin-top:0px; margin-bottom:9px; color:rgb(64,64,64); font-family:'Microsoft Yahei'; line-height:28px">
</p><pre name="code" class="css"> .clearfix:before,
.clearfix:after {
content: ".";
display: block;
height: 0;
visibility: hidden;
}
.clearfix:after {clear: both;}
.clearfix {zoom: 1;} /* IE < 8 */</pre><p></p>
<p style="margin-top:0px; margin-bottom:9px; color:rgb(64,64,64); font-family:'Microsoft Yahei'; line-height:28px">
<span style="white-space:pre"></span>其实只使用clearfix:after就可以达到清除浮动的效果,但只使用clearfix:after时在跨浏览器兼容问题会存在一个垂直边距叠加的bug,所以为了让浏览器兼容这个clearfix的清除浮动,在原来的基础上加止clearfix:before,这样就解决了跨浏览器的兼容问题。</p>
<p style="margin-top:0px; margin-bottom:9px; font-family:'Microsoft Yahei'; line-height:28px">
<span style="white-space:pre"></span>在这么多种清除浮动的方法中,都没有离开最原始的clear:both方法,特别是clearfix:after清除浮动,完全就是clear:both的一种变身,区别在于不需要在html增加一个标签,而只需要在有浮动元素的父元素中加上一个clearfix的class名,这样就轻松解决了清除浮动的问题。</p>

CSS 之 清除 float 常用的方法的更多相关文章

  1. div标签清除float浮动样式方法

    方法一. 这个方法来源于positioniseverything ,通过after伪类实现,完全兼容当前主流浏览器. 1 <style type="text/css"> ...

  2. css tips: 清除float影响,containing的div跟随floated sub等

    /** * For modern browsers * 1. The space content is one way to avoid an Opera bug when the * content ...

  3. 【转】CSS清除浮动_清除float浮动

    CSS清除浮动方法集合 一.浮动产生原因 一般浮动是什么情况呢?一般是一个盒子里使用了CSS float浮动属性,导致父级对象盒子不能被撑开,这样CSS float浮动就产生了. 浮动产生样式效果截图 ...

  4. CSS清除浮动_清除float浮动

    2.clear:both清除浮动为了统一样式,我们新建一个样式选择器CSS命名为“.clear”,并且对应选择器样式为“clear:both”,然后我们在父级“</div>”结束前加此di ...

  5. CSS清除float浮动

    一.浮动产生原因   -   TOP 一般浮动是什么情况呢?一般是一个盒子里使用了CSS float浮动属性,导致父级对象盒子不能被撑开,这样CSS float浮动就产生了. 本来两个黑色对象盒子是在 ...

  6. CSS清除浮动_清除float浮——详解overflow:hidden 与clear:both属性

    最近刚好碰到这个问题,看完这个就明白了.写的很好,所以转载了! CSS清除浮动_清除float浮动 CSS清除浮动方法集合 一.浮动产生原因   -   TOP 一般浮动是什么情况呢?一般是一个盒子里 ...

  7. css中的float属性以及清除方法 (2011-09-03 17:36:26)

    CSS里面的浮动属性是布局的常用工具,只有真正了解它并熟练使用才能将它的优点发挥到极致. 许多页面中都有文字绕图效果,并且各区块分布得错落有置,很多朋友在自学CSS布局时为了做出这些效果往往会被div ...

  8. css 清除float的方法

    首先我们要理解这个flaot 为什么要清除,作为小白来说直接颠覆了我之前学的内容,因为之前学的东西虽然碰到float后,脱离文档流后给兄弟元素或者父元素造成影响,但是都是通过option来定位 要么绝 ...

  9. 关于CSS中清除浮动的方法

    在使用CSS样式时会经常使用到浮动(float),这时如果没有清除浮动就会造成很多怪异的现象,因此对父级元素清除浮动是必须要做的,这样也是书写CSS的一个良好习惯. 目前常用的方法大致有三种. (1) ...

随机推荐

  1. Hessian介绍

    Hessian是什么   Hessian类似Web Service,是一种高效简洁的远程调用框架. Hessian的主页:http://hessian.caucho.com/   有关网上的对Hess ...

  2. Python脚本控制的WebDriver 常用操作 <五> 访问链接

    下面将使用webdriver来访问一个web链接 测试用例场景 测试中,经常会点击几个链接来进行操作,所以访问链接是基本的常见操作 Python脚本 from selenium import webd ...

  3. opencv linux

    This link which you also mentioned describes the necessary steps to compile OpenCV on your machine. ...

  4. 内省与JavaBean

    概述 JavaBean代表一类特殊的Java类,这种类主要用来存储和传递属性信息,JavaBean中的方法主要用于设置和获取这些私有属性,他们有一定的命名规则,我们可以把它们想象为一个侧重属性信息的类 ...

  5. wuzhicms 自定义SQL 标签

    {wz:sql sql="select * from wz_guestbook"} {loop $rs $r} {$r[title]} {/loop} {/wz} 自定义统计条数: ...

  6. NOIP2014 无线网络发射器选址

    1.无线网络发射器选址 (wireless.cpp/c/pas) [问题描述] 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平 ...

  7. 对String的一点了解

    /** * @param args */ public static void main(String[] args) { String str1 = "welcome"; Str ...

  8. 【Spark学习】Apache Spark安全机制

    Spark版本:1.1.1 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4135808.html 目录 W ...

  9. Java之Property-统获取一个应用程序运行的次数

    package FileDemo; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStre ...

  10. 集合框架Map之entrySet方法的使用

    Map的entrySet函数的使用,取得是键和值的映射关系,Entry就是Map接口中的内部接口,类似与我们熟悉的内部类一样,内部类定义在外部类内部,可以直接访问到外部类中的成员 package cn ...