巧用clear:both

本文介绍了解决CSS布局中浮动导致的问题的方法,详细解释了如何使用clear:both清除浮动,并提供了具体的代码示例。

我们在制作网页中用div+css或者称xhtml+css都会遇到一些很诡异的情况,明明布局正确,但是整个画面却混乱起来了,有时候在IE6下看的很正常的,到ie7或者火狐下看时,就一片混乱了,无论怎么计算,就是不能将排版改正过来。其实,这一切都是浮动搞得鬼,也就是css中的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>

对于因多加的<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>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>CSS-盒子浮动与定位</title> <style> body{ background-image: url(pic/bg1.jpg); font-family: Verdana, Geneva, sans-serif; font-size: 13px; color: darkgray; line-height: 1.5; margin: 0; } #father{ width:900px; background-color:white; background-image:url(pic/logo.gif); margin-left:auto; margin-right:auto; height:150px; background-repeat:no-repeat; background-position:right top; height:1100px; } #intro{ width:300px; border:1px gray dotted; margin:5px; padding:5px; float:left; } #intro img{ width:280px; height:220px; border:1px gray solid; } #scene{ width:555px; border:1px gray dotted; margin:5px; padding:5px; float:right; background-image:url(pic/bg3.jpg); margin-top:150px; position: relative; z-index: 1; } #scene img{ width:200px; height:170px; border:5px gray solid; float:left; margin-right: 10px; margin-bottom:10px; } #scene1,#scene2{ clear:both; width:450px; border-top:1px skyblue dashed; margin:10px auto; padding-top: 10px; } #food{ float:left; width:300px; border:1px gray dotted; margin-left:5px; padding:5px; clear: left; } #food img{ width:280px; height:220px; border:5px gray solid; } #pic6{ position:relative; top:150px; } .title1 h3, .title3 h3 { text-indent: -9999px; height: 0; margin: 0; padding: 0; } .title1 { background-image: url(pic/title1.gif); background-repeat: no-repeat; height: 70px; margin-bottom: 10px; } .title3 { background-image: url(pic/title3.gif); background-repeat: no-repeat; height: 40px; margin-bottom: 10px; } #extra-img { position: absolute; top: 500px; left: 500px; width: 150px; height: 100px; background-image: url(pic/bg0.gif); background-repeat: no-repeat; z-index: 2; }body{ background-image: url(pic/bg1.jpg); font-family: Verdana, Geneva, sans-serif; font-size: 13px; color: darkgray; line-height: 1.5; margin: 0; } #father{ width:900px; background-color:white; background-image:url(pic/logo.gif); margin-left:auto; margin-right:auto; height:150px; background-repeat:no-repeat; background-position:right top; height:1100px; } #intro{ width:300px; border:1px gray dotted; margin:5px; padding:5px; float:left; } #intro img{ width:280px; height:220px; border:1px gray solid; } #scene{ width:555px; border:1px gray dotted; margin:5px; padding:5px; float:right; background-image:url(pic/bg3.jpg); margin-top:150px; position: relative; z-index: 1; } #scene img{ width:200px; height:170px; border:5px gray solid; float:left; margin-right: 10px; margin-bottom:10px; } #scene1,#scene2{ clear:both; width:450px; border-top:1px skyblue dashed; margin:10px auto; padding-top: 10px; } #food{ float:left; width:300px; border:1px gray dotted; margin-left:5px; padding:5px; clear: left; } #food img{ width:280px; height:220px; border:5px gray solid; } #pic6{ position:relative; top:150px; } .title1 h3, .title3 h3 { text-indent: -9999px; height: 0; margin: 0; padding: 0; } .title1 { background-image: url(pic/title1.gif); background-repeat: no-repeat; height: 70px; margin-bottom: 10px; } .title3 { background-image: url(pic/title3.gif); background-repeat: no-repeat; height: 40px; margin-bottom: 10px; } #extra-img { position: absolute; top: 500px; left: 500px; width: 150px; height: 100px; background-image: url(pic/bg0.gif); background-repeat: no-repeat; z-index: 2; }body{ background-image: url(pic/bg1.jpg); font-family: Verdana, Geneva, sans-serif; font-size: 13px; color: darkgray; line-height: 1.5; margin: 0; } #father{ width:900px; background-color:white; background-image:url(pic/logo.gif); margin-left:auto; margin-right:auto; height:150px; background-repeat:no-repeat; background-position:right top; height:1100px; } #intro{ width:300px; border:1px gray dotted; margin:5px; padding:5px; float:left; } #intro img{ width:280px; height:220px; border:1px gray solid; } #scene{ width:555px; border:1px gray dotted; margin:5px; padding:5px; float:right; background-image:url(pic/bg3.jpg); margin-top:150px; position: relative; z-index: 1; } #scene img{ width:200px; height:170px; border:5px gray solid; float:left; margin-right: 10px; margin-bottom:10px; } #scene1,#scene2{ clear:both; width:450px; border-top:1px skyblue dashed; margin:10px auto; padding-top: 10px; } #food{ float:left; width:300px; border:1px gray dotted; margin-left:5px; padding:5px; clear: left; } #food img{ width:280px; height:220px; border:5px gray solid; } #pic6{ position:relative; top:150px; } .title1 h3, .title3 h3 { text-indent: -9999px; height: 0; margin: 0; padding: 0; } .title1 { background-image: url(pic/title1.gif); background-repeat: no-repeat; height: 70px; margin-bottom: 10px; } .title3 { background-image: url(pic/title3.gif); background-repeat: no-repeat; height: 40px; margin-bottom: 10px; } #extra-img { position: absolute; top: 500px; left: 500px; width: 150px; height: 100px; background-image: url(pic/bg0.gif); background-repeat: no-repeat; z-index: 2; }body{ background-image: url(pic/bg1.jpg); font-family: Verdana, Geneva, sans-serif; font-size: 13px; color: darkgray; line-height: 1.5; margin: 0; } #father{ width:900px; background-color:white; background-image:url(pic/logo.gif); margin-left:auto; margin-right:auto; height:150px; background-repeat:no-repeat; background-position:right top; height:1100px; } #intro{ width:300px; border:1px gray dotted; margin:5px; padding:5px; float:left; } #intro img{ width:280px; height:220px; border:1px gray solid; } #scene{ width:555px; border:1px gray dotted; margin:5px; padding:5px; float:right; background-image:url(pic/bg3.jpg); margin-top:150px; position: relative; z-index: 1; } #scene img{ width:200px; height:170px; border:5px gray solid; float:left; margin-right: 10px; margin-bottom:10px; } #scene1,#scene2{ clear:both; width:450px; border-top:1px skyblue dashed; margin:10px auto; padding-top: 10px; } #food{ float:left; width:300px; border:1px gray dotted; margin-left:5px; padding:5px; clear: left; } #food img{ width:280px; height:220px; border:5px gray solid; } #pic6{ position:relative; top:150px; } .title1 h3, .title3 h3 { text-indent: -9999px; height: 0; margin: 0; padding: 0; } .title1 { background-image: url(pic/title1.gif); background-repeat: no-repeat; height: 70px; margin-bottom: 10px; } .title3 { background-image: url(pic/title3.gif); background-repeat: no-repeat; height: 40px; margin-bottom: 10px; } #extra-img { position: absolute; top: 500px; left: 500px; width: 150px; height: 100px; background-image: url(pic/bg0.gif); background-repeat: no-repeat; z-index: 2; } </style> </head> <body> <div id="father"> <div id="intro"> <div class="title1"><h3>阳朔概况</h3></div> <img class="pic1" src="pic/4.jpg" /> <p >阳朔县位桂林市区东南,隶属广西桂林市,位于漓江西岸,风景秀丽。建县始于隋开皇十年(公元590年),距今已1400余年。县城距桂林市区65公里,居住着汉、壮、瑶、回等13个民族。</p> </div><!--intro end--> <div id="scene"> <div class="title2"><h3>阳朔风光</h3></div> <img class="pic2" src="pic/2.jpg" /> <p class="text">“桂林山水甲天下,阳朔山水甲桂林”,高度概括了阳朔的自然风光在世界上所占有的重要位置。“山青、水秀、峰奇、洞巧”,为天下四绝。阳朔百里山川,处处奇山秀水,自然景观与人文景观交相辉映,被誉为“中国旅游名县”。</p> <div id="scene1"> <div class="title21"><h3>阳朔漓江景区</h3></div> <img class="pic3" src="pic/1.jpg" /> <p>这一景区江水清澄见底,宛如罗带的漓江蜿蜒于苍翠雄奇的群山之间,人称“百里漓江,百里画廊”。江边名山密集、千姿百态、翠竹丛丛,村落屋宇掩映在绿树丛中,构成一幅美丽的山水长轴,宛若一道千回百转的画廊。县境自杨堤冠岩村顺江而下,主要景点有:桃源仙境、浪石胜境、仙人推磨、绣山彩壁、鸳鸯戏水、九马画山、七姐下凡、螺蛳山等二十多处。乘舟游览这一景区,好似遨游仙宫,如入梦境,“分明看见青山顶,船在青山顶上行”。</p> </div><!--scene1 end--> <div id="scene2"> <div class="title22"><h3>遇龙河景区</h3></div> <img class="pic4" src="pic/3.jpg" /> <p>遇龙河是阳朔县第二大河,全长42.5公里,至书童山汇入漓江。河两岸奇峰秀丽。竹木苍翠、水质清澈、景色宜人。主要景点有:犀牛塘、水厄风光、五指山、朝阳寨、仙桂桥、遇龙桥、归义古城遗址、汉墓群、东晕岩、八仙过海等近20处。遇龙河田园风光旅游,最适宜采用三、五成群结伴徒步的游赏方式。遇龙河水清见底,一切都是静悄悄的,偶有飞鸟鸣聚,水车吱吱呀呀,旅游者可感受真正“诗境家园”的意韵。</p> </div><!--scene2 end--> </div><!--scene end--> <div id="food"> <div class="title3"><h3>美食</h3></div> <img class="pic5" src="pic/7.jpg" /> <p>漓江啤酒鱼:不吃不知道,一吃吓一跳,大名如雷贯耳,如果不试一下就真的枉费到此一游。啤酒鱼有点像川菜中火锅鱼的做法,先把鱼在油里煎一煎,再放啤酒及各种调料烧制而成。烧的器具很特别,鱼放在一个平底的盘子里,盘子下面是一个盆子,放了些水,再下面才是火,而不像是川菜火锅直接将锅放在炉火上面。这样上面的鱼不易烧糊且受热均匀,入口果然鲜辣可口,无一丝鱼本身的腥味。对了,一定要吃带鳞的,正宗的啤酒鱼,鱼鳞都炸得非常脆。好吃极了。</p> </div><!--food end--> </div><!--father end--> </body> </html> 为什么bg0不能显示
11-06
### CSS `clear: both` 属性详解 #### 定义与作用 `clear: both` 是一种常用的CSS属性值,主要用于处理HTML文档中的浮动元素。当页面内的子元素应用了`float`样式之后,可能会导致父容器的高度无法自动适应这些浮动子元素,进而影响布局效果[^1]。 #### 实际应用场景 假设存在一个包含两个子盒子的大盒子,在这两个子盒子上设置了不同的浮动方向(左侧和右侧),如果不加以控制,则可能导致大盒子内部结构错乱,例如背景颜色或边框未能正常包裹住所有内容。此时通过给定特定的选择器添加`clear: both`可以有效解决此类问题[^2]。 ```css /* 外层容器 */ .parent-container { border: 1px solid black; } /* 左侧漂浮的小盒子 */ .left-floated-box { float: left; width: 100px; height: 100px; background-color: lightblue; } /* 右侧漂浮的小盒子 */ .right-floated-box { float: right; width: 100px; height: 100px; background-color: lightcoral; } /* 清除浮动的影响 */ .clearfix::after { content: ""; display: table; clear: both; } ``` 上述代码片段展示了如何利用伪类`:after`配合`content`以及`display:table`来创建一个新的匿名表格单元格,并对其施加`clear:both`规则,从而达到清除浮动的效果[^3]。 #### 常见疑问解答 - **为什么会出现浮动塌陷?** 浮动元素会脱离标准流,使得其后的兄弟节点按照原本的位置排列而不考虑前面的浮动元素位置;而如果父级元素没有任何非浮动的内容撑起高度的话就会发生所谓的“塌陷”。 - **除了使用clearfix外还有其他方式吗?** 当然还可以采用额外标签法即在最后面增加一个空div并指定style="clear:both;",不过这种方法不够优雅也不推荐广泛运用因为增加了无意义标记。 - **现代浏览器支持情况怎么样呢?** 几乎所有的主流浏览器都完全兼容此特性,开发者无需担心跨平台适配性方面的问题。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值