clear:both;
CSS手册上这样解释的:该属性的值指出了不允许有浮动对象的边。
这个属性是用来控制float属性在文档流的物理位置的。
当属性设置float(浮动)时,他所在的物理位置已经脱离文档流了,但是大多时候我们希望文档流能识别float(浮动),或者是希望float(浮动)后面的元素不被float(浮动)所影响,这个时候我们就需要用clear:both;来清除clear:both;
例子:
<p style="float:left;width :100px;">这个是第1列,</p>
<p style="float:left;width :400px;">这个是第2列,</p>
<p >这个是列的下面。</p>
如果不用清除浮动,那么第3个<P>的文字就会和第一二行在一起
所以我们在第3个这利加一个清除浮动。
<p style="float:left;width :100px;">这个是第1列,</p>
<p style="float:left;width :400px;">这个是第2列,</p>
<p clear:both;>这个是列的下面。</p>
CSS中 clear:both;可以终结在出现他之前的浮动 CSS中 clear:both;可以终结在出现他之前的浮动
语法: clear : none | left |right | both
参数:
none : 允许两边都可以有浮动对象
both : 不允许有浮动对象
left : 不允许左边有浮动对象
right : 不允许右边有浮动对象
说明: 该属性的值指出了不允许有浮动对象的边。请参阅float属性。 对应的脚本特性为clear
示例:
div { clear : left }
img { float: right }
<div style="clear:both;"></div>
主要是用在div套div的结构中。如果内div是浮动的,一般都需要clear浮动,不然的话内div会超出外div的框架
所用什么时候用clear:both;就很重要,一般我们在需要清除浮动的时候用到clear:both;不要轻意用到clear:both;因为它也有副伯用.
我们写一个clear:both;的例子:
<div style="float:left;width:100px;"> clear:both第1行第1列,</div>
<div style="float:left;width:700px;"> clear:both第1行第2列,</div>
<div style="clear:both;"> clear:both第2行。</div>
效果:
clear:both第1行第1列,clear:both第1行第2列,clear:both第2行。
IE6下的{clear:both}出现怪异的空白做浮动的时候经常要用{clear:both;},于是将该定义为一个全局的类,
.clear{clear:both; height:0px; margin:0; padding:0; width:0; border:none;}
但是在实际的使用过程中,IE6下会发生一个怪异的现象,<div class="clear"></div>,这样的清格式的层也会占用高度,但是类定义里已经将高度设置为0了,这里为什么还会出现这样的现象呢?
加上这样一个定义,就一切OK了!
.clear{ clear:both; height:0px; margin:0; padding:0; width:0; border:none; overflow:hidden; }
推荐阅读
overflow:hidden.>>>详细阅读
本文标题:clear:both; 的作用
地址:http://www.17bianji.com/diaocha/34320.html
1/2 1