展会信息港展会大全

css 两种清除浮动经典实例代码
来源:互联网   发布日期:2016-03-25 15:56:20   浏览:1830次  

导读:一、使用空标签清除浮动<style type=”text/css”> <!– *{margin:0;padding:0;} body{font:36px bold; ...

一、使用空标签清除浮动

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

</div>

空标签可以是div标签,也可以是p标签。我习惯用<p>,够简短,也有很多人用<hr>,只是需要另外为其清除边框,但理论上可以是任何标签。这种方式是在需要清除浮动的父级元素内部的所有浮动元素后添加这样一个标签清楚浮动,并为其定义css代码:clear:both。此方法的弊端在于增加了无意义的结构元

使用overflow属性

有效地解决了通过空标签元素清除浮动而不得不增加无意代码的弊端。使用该方法是只需在需要清除浮动的元素中定义css属性:overflow:auto,即可1zoom:1′用于兼容ie6。

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

赞助本站

人工智能实验室

相关热词: 开发 编程 android

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港