清除:均在块结束之前
通常,我们最终会做这样的事情:
<div class="one">
<div class="floating-two">content</div>
<div class="clear"></div>
</div>
我们通常的意思是,“确保所有浮动元素都包含在块一中”。因此,应用于“一”的任何背景都可能出现在所有内容后面,无论是否浮动。
我正在寻找一种更清洁的方法来做到这一点。毕竟,“clear”div 只是我们打算应用于“one”的一种样式。我们可以这样做:
.floating-two:after{
clear:both;
}
但这也不正确。 Floating-two 不知道在“one”关闭之前是否有其他块跟随在它后面。
有人为此开发过一种技巧吗?
So often, I we wind up doing something like this:
<div class="one">
<div class="floating-two">content</div>
<div class="clear"></div>
</div>
What we usually mean is, "make sure that any floated elements are included into block one". Sof potentially, any background applied to "one" appears behind everything, floating or not.
I am looking for a cleaner way to do this. After all, the "clear" div is simply a style that we intend to apply to "one". We could do:
.floating-two:after{
clear:both;
}
But this is not correct either. Floating-two doesn't know whether there might be other blocks following it before "one"'s closure.
Has anyone developed a trick for this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过两种方式清除此操作,而无需添加标记(没有
或等效项):
或者
You can clear do this in two ways without adding markup (without a
<div class="clear"..>
or equiv):OR
您可以在外部 div 上使用
overflow:hidden
,它将拉伸到包含.floating-two
所需的高度。这里摆弄的示例: http://jsfiddle.net/neilheinrich/rBBMp/6/You can use
overflow: hidden
on the outer div and it will stretch to the height needed to contain.floating-two
. Example fiddled here: http://jsfiddle.net/neilheinrich/rBBMp/6/您必须添加具有
clear
属性的真实元素。如果您不喜欢这些 div,请使用
。它是最短、有效的元素,不会修改布局:You have to add a real element with the
clear
property. If you don't like the divs, use a<br>
. It's the shortest, valid element which doesn't modify the lay-out:编辑
我确实相信 RobW 说你不能用 :after 做到这一点是正确的,但你可以用这个 css 使它更加动态:
http://jsfiddle.net/qQaQg/2
EDIT
I do believe that RobW is correct in saying you can't do this with :after, but you can make it more dynamic with this css:
http://jsfiddle.net/qQaQg/2