清除浮动“clear:both;”的应用

| |
[不指定 2008/04/13 20:47 | by Else ]
在CSS中我们会经常要用到“清除浮动”Clear,比较典型的就是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;
代码:
复制代码 | 运行代码 | 另存代码
<p style="float:left;width:200px;">这个是第1列,</p>
<p style="float:left;width:400px;">这个是第2列,</p>
<p style="clear:both;">这个是第3列。</p>


通常,我们往往会将“清除浮动”单独定义一个CSS样式,如:





.clear {
    clear: both;
}



然后使用
来专门进行“清除浮动”。

不过也有不赞同意见是,
可以不写,直接在下层清除就可以了。

比如本来好好的


<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>


[codes=CSS].clear {
    clear: both;
    height:1px;
    margin-top:-1px;
    overflow:hidden;
}[/codes]

代码:
复制代码 | 运行代码 | 另存代码
<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>



作者:Else 's Blog
地址:http://www.aixq.com/post/999/
版权所有。转载时必须链接形式注明作者和原始出处及本声明!
  • 中查看更多“清除浮动“clear:both;”的应用”相关内容
  • 中查看更多“清除浮动“clear:both;”的应用”相关内容
  • 中查看更多“清除浮动“clear:both;”的应用”相关内容
  • 中查看更多“清除浮动“clear:both;”的应用”相关内容
  • 中查看更多“清除浮动“clear:both;”的应用”相关内容
  • 中查看更多“清除浮动“clear:both;”的应用”相关内容
  • Tags:
    代码 程序 编程 | 评论(0) | 引用(0) | 阅读(748)
    发表评论
    表情
    emotemotemotemotemot
    emotemotemotemotemot
    emotemotemotemotemot
    emotemotemotemotemot
    emotemotemotemotemot
    打开HTML
    打开UBB
    打开表情
    隐藏
    昵称   密码   游客无需密码
    网址   电邮   [注册]