说明:使用xhtml+css布局经常性地会使用到float,很多邪门的事儿都有可能是浮动在作怪,那么清除浮动就是必须要做的,而且随时性地对父级元素清除浮动的做法也被认为是书写CSS的良好习惯之一。
常用的清除浮动的方法有以下三种:
此为未清除浮动源代码,运行代码无法查看到父级元素浅黄色背景。
<style type=”text/css”>
<!–
*{margin:0;padding:0;}
body{font:36px bold; color:#F00; text-align:center;}
#layout{background:#FF9;}
#left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;}
#right{float:right;width:30%;height:80px;background:#DDD;line-height:80px;}
–>
</style>
<div id=”layout”>
<div id=”left”>Left</div>
<div id=”right”>Right</div>
</div>
未清除浮动前如图所示:
三种清除浮动方法如下:
ps:<br clear=”all”/>也可以实现效果,但不清楚使用哪个比较好。呵呵
<style type=”text/css”>
<!–
*{margin:0;padding:0;}
body{font:36px bold; color:#F00; text-align:center;}
#layout{background:#FF9;}
#left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;}
#right{float:right;width:30%;height:80px;background:#DDD;line-height:80px;}
.clr{clear:both;}
–>
</style>
<div id=”layout”>
<div id=”left”>Left</div>
<div id=”right”>Right</div>
<p class=”clr”> </p>
</div>
《!————-
很多情况使用了float浮动之后,元素会发生错位,可能在IE下正常显示,但是在火狐和谷歌不能正常显示。这一切都是float在捣鬼。要解决此问题多数使用clear:both;
CSS手册上是这样说明的:该属性的值指出了不允许有浮动对象的边。这个属性是用来控制float属性在文档流的物理位置的。
当元素设置为float之后时,元素已经脱离了文档流,但是大多数情况,我们希望文档流能够识别float,或者float后面的元素不受float影响,这时候我们就要使用clear:both;
程序代码:
<p style=”float:left;width:200px;”>这个是第1列,</p>
<p style=”float:left;width:400px;”>这个是第2列,</p>
<p>这个是第3列。</p>
如果不用清除浮动,那么第3列文字就会和第1、2列文字在一起 ,所以我们在第3个这列加一个清除浮动 clear:both;
通常,我们往往会将“清除浮动”单独定义一个CSS样式,如:
程序代码
.clear {
clear: both;
}
然后使用<div class=”clear”></div>来专门进行“清除浮动”。
不过也有不赞同意见是,<div class=”clear”></div>可以不写,直接在下层清除就可以了。
比如本来好好的
程序代码
<p style=”float:left;width:200px;”>这个是第1列,</p>
<p style=”float:left;width:400px;”>这个是第2列,</p>
<p style=”clear:both;”>这个是第3列。</p>
非要整成
程序代码
<p style=”float:left;width:200px;”>这个是第1列,</p>
<p style=”float:left;width:400px;”>这个是第2列,</p>
<div class=”clear”></div>
<p>这个是第3列。</p>
这点看来,<div class=”clear”></div>确实不需要写。
不过很显然,我们在网页设计时还有一种很普遍的情况:
程序代码
<style type=”text/css”>
#main {background-color: #3399CC;width: 600px;padding: 20px;}
#sidebar {background-color: #FF6600; float: left;width: 130px;}
#container {float: right;width: 420px;background-color: #FFFF33;}
</style>
<div id=”main”>
<div id=”sidebar”>第一段内容 第一段内容 第一段内容</div>
<div id=”container”>第二段内容 第二段内容 第二段内容</div>
</div>
<p style=”clear:both;”>第三段内容</p>
该页面测试在IE下效果正合所要:蓝色块内部有红色和黄色两个色块内容,同时在蓝色块以下是第三段文本。
不过FF的效果可不是这样的。我们不能单单想在下一层清除就能完成我们的工作,我们必须在及时进行“清除”。
程序代码
<style type=”text/css”>
#main {background-color: #3399CC;width: 600px;padding: 20px;}
#sidebar {background-color: #FF6600; float: left;width: 130px;}
#container {float: right;width: 420px;background-color: #FFFF33;}
.clear {clear: both;}
</style>
<div id=”main”>
<div id=”sidebar”>第一段内容 第一段内容 第一段内容</div>
<div id=”container”>第二段内容 第二段内容 第二段内容</div>
<div class=”clear”></div>
</div>
<p>第三段内容</p>
程序代码
clear {
clear: both;
height:1px;
margin-top:-1px;
overflow:hidden;
}
程序代码
<style type=”text/css”>
#main {background-color: #3399CC;width: 600px;padding: 20px;}
#sidebar {background-color: #FF6600; float: left;width: 130px;}
#container {float: right;width: 420px;background-color: #FFFF33;}
.clear {clear: both;height:1px;margin-top:-1px;overflow:hidden;}
</style>
<div id=”main”>
<div id=”sidebar”>第一段内容 第一段内容 第一段内容</div>
<div id=”container”>第二段内容 第二段内容 第二段内容</div>
<div class=”clear”></div>
</div>
<p>第三段内容</p>
—————————–》
2、使用overflow属性。
<style type=”text/css”>
<!–
*{margin:0;padding:0;}
body{font:36px bold; color:#F00; text-align:center;}
#layout{background:#FF9;overflow:auto;zoom:1;}
#left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;}
#right{float:right;width:30%;height:80px;background:#DDD;line-height:80px;}
–>
</style>
<div id=”layout”>
<div id=”left”>Left</div>
<div id=”right”>Right</div>
</div>
3、使用after伪对象清除浮动。
该方法只适用于非IE浏览器。具体写法可参照以下示例。使用中需注意以下几点。一、该方法中必须为需要清除浮动元素的伪对象中设置height:0,否则该元素会比实际高出若干像素;二、content属性是必须的,但其值可以为空,蓝色理想讨论该方法的时候content属性的值设为”.”,但我发现为空亦是可以的。
<style type=”text/css”>
<!–
*{margin:0;padding:0;}
body{font:36px bold; color:#F00; text-align:center;}
#layout{background:#FF9;}
#layout:after{display:block;clear:both;content:””;visibility:hidden;height:0;}
#left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;}
#right{float:right;width:30%;height:80px;background:#DDD;line-height:80px;}
–>
</style>
<div id=”layout”>
<div id=”left”>Left</div>
<div id=”right”>Right</div>
</div>
清除浮动后如图所示:
此三种方法各有利弊,使用时应择优选择,比较之下第二种方法更为可取。