巧用clear:both
我们在制作网页中用div+css或者称xhtml+css都会遇到一些很诡异的情况,明明布局正确,但是整个画面却混乱起来了,有时候在IE6下看的很正常的,到ie7或者火狐下看时,就一片混乱了,无论怎么计算,就是不能将排版改正过来。其实,这一切都是浮动搞得鬼,也就是css中的float,要解决情况,就需要使用clear:both了。
程序代码: <p style="float:left;width:200px;">这个是第1列,</p> 如果不用清除浮动,那么第3列文字就会和第1、2列文字在一起 ,所以我们在第3个这列加一个清除浮动 clear:both; 程序代码
.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> 对于因多加的<div class="clear"></div>标签会引起IE和FF高度变化,通过如下方法解决: 程序代码
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> |
PS:我的淘宝店铺新开业,经营各种桌游,棋牌,希望大伙儿能来看看!http://201314yes.taobao.com/
posted on 2012-03-28 18:49 jenney.qiu 阅读(41403) 评论(1) 编辑 收藏 举报