打印

[教程] [转载]Simple Clearing of Floats(清除浮动的新方法)

先把这里处理的CSS代码贴出来。文章是英文的,不过很好懂,我就直接贴了。

 提示:您可以先修改部分代码再运行
Simple Clearing of Floats(正文)

by Alex Walker

Without wanting to stray too deeply in Simon and Stuart’s technical CSS territory, I thought this was worth noting.

For all it’s many advantages, sometimes it’s the little things that CSS layout makes difficult that really get to you. Clearing floated elements is a good example.

The Problem:

One of the simplest and most common layout structures involves the placing of a small, set-width DIV — perhaps navigation, a quote or a bio — within a larger wrapping DIV that contains the remaining content. In a markup this might be something like:


1 <div id="outer">   
2  <div id="inner"> <h2>A Column</h2> </div>   
3  <h1>Main Content</h1>   
4  <p>Lorem ipsum</p>   
5 </div>  
view plain | print | copy to clipboard
<div id="outer">
<div id="inner"> <h2>A Column</h2> </div>
<h1>Main Content</h1>
<p>Lorem ipsum</p>
</div>We can set the width of ‘#inner’ (let’s say ‘width:20%’), but, being a block level element, the main content will always wrap beneath, unless we float it (either left or right). Here our classic problem begins.

If ‘#inner’ is shorter than #outer, all is well.

However, if ‘#inner’ grows taller than it’s wrapping parent, it breaks through the bottom edge of ‘#outer’. It’s as if ‘#outer’ forgets it’s supposed to be keeping tabs on what ‘#inner’ is doing as soon as you float it.

As we can’t always control the amount of content in these DIVs, it certainly presents a problem. Here’s a typical example of the problem in action. (Thanks to Pixy for the neat little content-gen script).

The Solutions:

a) The Markup Method: The first and W3C-recommended approach is a little ugly - extra markup. At the very end of your content, toss in a cleared element - typically something like <br style="clear:both"/>. It’s the HTML equivalent of wedging matchsticks into your window frame to jam a window open. This works, but ‘dirties your page’ with stuff that only exist so it renders properly.

b) The Aslett/PIE Method: Less than 12 months ago Tony Aslett working with PositionIsEverything.com came out with a new method so diabolically clever that they had to have been sitting in a fake island volcano, stroking a large white cat and laughing fiendishly when they thought of it.

You’ll need to read the tutorial to get the full story, but, in short, they use a little-known, rarely-used pseudo class (:after) to place a hidden, cleared full-stop after the content. Combined with a sprinkling of hacks, this works beautifully - but gives me a headache over my left eye when I think about it.

c) The Ordered List Method: Last October Steve Smith from Orderlist.com published a slightly simpler method. Again, read his tutorial to get the low-down, but in short, his method involves ‘Floating nearly Everything’ (FnE), which naturally enough includes the outer DIV. This can have a considerable effect on the way your design stacks and as Steve says ‘it takes a little more tweaking’ but in general this method seems a little more robust to me.

d) That was my ‘current state of play’ until last week when SitePoint Forum’s own CSS Guru, Paul O’Brien, nonchalantly pointed out that adding a ‘overflow:auto’ to the outer DIV did the trick.

‘Rubbish’ I thought to myself.

Half an hour of testing later, I was amazed to find Paul was 100% correct - as this example shows. It seems that reminding the outer DIV that it’s overflow is set to ‘auto’, forces it to think “oh yeah.. I’m wrapping that thing, aren’t I?”.

This fact is so boringly simple that it’s hard to know if thousands of developers are well aware of it, but never thought to mention it. I know I showed the example page to four CSS-savvy SitePoint colleagues and all shock their heads, blinked slowly and said “Whaa…?” (or something similar).

From my testing, it seems to work identically in virtually every browser. Even IE4 seems to love it - only NS4 freaks out, and I’m not totally convinced a few hacks couldn’t get that working.

We haven’t had time yet to thoroughly test this method under rigorous match conditions, but so far there don’t seem to be any major drawbacks.

Certain combinations of margin and padding can force internal scrollbars. If you can’t ‘massage’ them away, we found ‘overflow:hidden’ has virtually the same effect without the scrollbars. The only drawback of ‘hidden’ seems to be the cropping of some images if they’re placed lower in the page.

Both issues seem very manageable.

Nice work, Paul.
海啸的地盘,有空来看看
我英文不好....大概看了下好像重点是d .....
恩!overflow只要不是 visible就能够触发了div layout 模式 自动闭合float ...
以前好像有篇文章就说过这个问题.....好像是old9大大写的!
在论坛里面找找吧!!我上面的说法不好!
哇,让我兴奋的是圆角代码"-moz-border-radius: 0 0 15px 0;"在ff3里渲染的最好,没有毛边,ff2是锯齿边缘,ie和safari不支持.另,恕我浅薄,清楚浮动用"clear:both;"有什么特别吗?

回复 Amyni 在 3# 的帖子

这篇文章不是说明用clear来清除浮动,是用overflow:auto
海啸的地盘,有空来看看

TOP

还在为头像烦恼?还在为不能关注好友动态烦忧?快来蓝色理想家园吧!
顶一下姚哥的帖子。

这种方法的确比较快捷,不过在使用超出容器范围的绝对定位时会出现BUG:

 提示:您可以先修改部分代码再运行
我感觉目前最好的清除浮动方法还是:
浮动 {zoom:1}
浮动:after {content:".";display:block;height:0;clear:both;visibility:hidden;}

例子:

 提示:您可以先修改部分代码再运行
玛雅老班底:39454127

TOP

很老的方法了,最好不要使用“新方法”作为标题--!被骗了
个人Blog:PlanABC   团队Blog:淘宝UED  专注Web前端技术!

TOP

回复 blank 在 6# 的帖子

呵呵,不这样怎么能够吸引人啊!!
海啸的地盘,有空来看看

TOP

....标题党。那就在复习一次好了。

TOP

回复 majer 在 8# 的帖子

呵呵,其实这个方法还是不错的,给刚开始学习的朋友,应该是很不错的一个清除浮动的技巧了。简单而且不会在有多余的代码了。
海啸的地盘,有空来看看

TOP

这种方法不敢恭维啊
!!!

TOP

还不错。

TOP

抱着好奇进来的,很失望。

清除浮动有些代码洁癖的人一般是在浮动元素的父容器上做文章,ie下给 layout ,标准浏览器用after伪类。

overflow用起来总有些提心吊胆。做表单的时候尤其注意焦点问题。

TOP

回复 zehee 在 12# 的帖子

我不知道大家为什么会这么想,overflow用起来很管用啊

http://www.yaohaixiao.com/samples/new-fg114/

我为公司的新首页就是用的这个,我以前也用after伪类,不过中觉得写的很累赘.overflow的兼容性很好的,我甚至IE5.5里也测试了.蛮好用的啊.不必要在用什么HACK了,不是很好吗?

当然还是看个人喜欢用什么方法了,不过多一个思路总归不是什么坏事.

[ 本帖最后由 yaohaixiao 于 2008-7-26 06:08 编辑 ]
海啸的地盘,有空来看看

TOP

这个圆角属性...要是ie都支持了那多爽呀。

TOP