大多数前端使用.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. [转] 在Asp.net前台和后台弹出提示框

    一.在前台弹出提示框 1.点击"A"标记或者"控件按钮"弹出提示框 <asp:LinkButton ID="lbtnDel" runa ...

  2. 一个简单的appium脚本

    这是一个简单的appium脚本,测试amazon购物过程的,过程包括搜索商品,然后从结果列表中选取中意的商品,然后加入购物车,等等. 它是一个最原始的脚本,对测试元素.数据.报告等未作任何的封装,后面 ...

  3. 设计模式_Proxy_代理模式

    形象例子: 跟MM在网上聊天,一开头总是“hi,你好”,“你从哪儿来呀?”“你多大了?”“身高多少呀?”这些话,真烦人,写个程序做为我的Proxy吧,凡是接收到这些话都设置好了自动的回答,接收到其他的 ...

  4. STM32使用以下规则对过滤器编号:

    STM32使用以下规则对过滤器编号:(1) FIFO_0和 FIFO_1的过滤器分别独立编号,均从0开始按顺序编号.(2) 所有关联同一个 FIFO 的过滤器,不管有没有被激活,均统一进行编号.(3) ...

  5. setBackgroundMusicLoop for cocos2dx

    给cocos2dx的SimpleAudioEngine加上设置背景音乐循环的方法,详见gist.

  6. cocos2d-x的helloLua例子函数名定义误导初学者

    初次研究cocos2d-x, cocos2d-x支持lua是一个很不错的功能,使用lua来开发有个最大的好处就是不用每次改了游戏代码都编译,大多数情况下改了脚本直接运行程序就可以了,发布更新时也不用更 ...

  7. varchar

    mysql varchar(50) 不管中文 还是英文 都是存50个的 MySQL5的文档,其中对varchar字段类型这样描述:varchar(m) 变长字符串.M 表示最大列长度.M的范围是0到6 ...

  8. Qt QDebug :Cannot retrieve debugging output!

    调试Qt程序时用Qdebug类输出调试信息:     qDebug("read My Com"); 这个问题是个小问题,其实跟程序没关系.当你同时开多个Qt程序(Creator编程 ...

  9. Theme使用的几点注意事项

    Theme.Holo主题在android4.0 及其以上不需要任何支持包,继承Activity即使用,但google不推荐该用法 Theme.Material主题在android5.0及其以上不需要依 ...

  10. 使用poi将word转换为html

    使用poi将word转换为html,支持doc,docx,转换后可以保持文字.表格.图片.样式 演示地址: https://www.xiaoyun.studio/app/preview.html 完整 ...