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