闭合浮动元素超级简单的方法

2022-04-15 0 737

无意中看到的一个非常不错的小技巧.是关于用CSS处理闭合浮动元素的方法,收藏了.

关于闭合浮动元素(clearing float)的方法现在已经很多了,你还不了解的话去old9的blog看看,有一篇闭合浮动元素。

这些方法我最喜欢就是 使用:after 伪类动态的嵌入一个用于清除浮动的元素,可惜代码量太大了,看着不够简洁。现在我看到有个方法超级简单。赶紧介绍一下。原文在:http://annevankesteren.nl/2005/03/clearing-floats

原理是这样的,外围元素之所以不能很好的延伸,问题出在了overflow上,因为overflow不可见。见W3C的解释

引用内容: In addition, if the element has any floating descendants whose bottom margin edge is below the bottom, then the height is increased to include those edges. Only floats that are children of the element itself or of descendants in the normal flow are taken into account, e.g., floats inside absolutely positioned descendants or other floats are not.

现在只要将给外围元素添加一个overflow:auto;就可以解决问题,结果是除了IE,真的可以解决。下来就要解决ie的问题了,再加上_height:1%; ,这个问题就完全解决了。

下面是我做的三个例子作为比较

1.没有闭合浮动元素

2.非IE下闭合浮动元素

3.完全闭合元素

<P><!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”><html xmlns=”http://www.w3.org/1999/xhtml”><head><meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ /><title>Super simple clearing floats</title><style type=”text/css”><!–body{ font:.9em/150% Verdana, Arial, Helvetica, sans-serif normal; color:#333333;}h1{ background:#f5f5f5; padding:6px;}em{ color:#f60; font-style:normal;}code{ display:block; font-family:”Courier New”, Courier, monospace; border:1px #ccc dotted; padding:10px;}</P> <P>#wrap_three{ border:6px #ccc solid; overflow:auto;_height:1%;} .column_left{ float:left; width:20%; padding:10px;} .column_right{ float:right; width:75%; padding:10px; border-left:6px #eee solid;}</P> <P>–></style></head></P> <P><body><div id=”wrap_three”> <div class=”column_left”> <h1>Float left</h1> <p>这个层左浮动</p> <code>.column_left{ float:left; width:20%; padding:10px;}</code> </div> <div class=”column_right”> <h1>Float right</h1> <p>这个层右浮动,下面是完整的CSS,这是一个外围wrap_three层应用<em>overflow:auto;</em>加<em>_height:1%;</em>闭合浮动的模型。外围容器可以正常延伸。IE6.0、Firefox 1.5和Opera 9.0都通过了测试。 </p> <code> #wrap_three{ border:6px #ccc solid; overflow:auto; _height:1%;} .column_left{ float:left; width:20%; padding:10px;} .column_right{ float:right; width:75%; padding:10px; border-left:6px #eee solid;}</code> </div></div></body></html></P>

[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 CSS/HTML 闭合浮动元素超级简单的方法 https://www.niceym.com/13381.html