码迷,mamicode.com
首页 > 其他好文 > 详细

围住浮动元素的三种方法

时间:2015-06-20 14:19:55      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:web前端

当有浮动元素时:

<section>
<img src="images/rubber_duck2.jpg">
<p>It‘s fun to float.</p>
</section>
<footer> Here is the footer element that runs across the bottom of the
page.</footer>
section {border:1px solid blue; margin:0 0 10px 0;}
img {float:left;}
footer {border:1px solid red;}

技术分享

有三种方法可以解决这个问题:

  • 为父元素添加overflow:hidden
section {border:1px solid blue; margin:0 0 10px 0; overflow:hidden;}
img {float:left;}
p {border:1px solid red;}
  • 同时浮动父元素
section {border:1px solid blue; float:left; width:100%;}
img {float:left;}
footer {border:1px solid red; clear:left;}
  • 添加非浮动的清除元素
<section>
<img src="images/rubber_duck.jpg">
<p>It‘s fun to float.</p>
<div class="clear_me"></div>
</section>
<footer> Here is the footer element…</footer>
section {border:1px solid blue;}
img {float:left;}
.clear_me {clear:left;}
footer {border:1px solid red;}

如果不想添加这个纯表现型元素,我再告诉你一个用css来添加这个清除元素的方法。首先给section添加一个类

<section class="clearfix">
<img src="images/rubber_duck.jpg">
<p>It‘s fun to float.</p>
</section>
<footer> Here is the footer element…</footer>
.clearfix:after {
content:".";
display:block;
height:0;
visibility:hidden;
clear:both;
}

围住浮动元素的三种方法

标签:web前端

原文地址:http://blog.csdn.net/u012193330/article/details/46573341

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!