CSS 中清除浮动的最佳元素?
我想知道清除浮动块级元素的最佳元素是什么?
目前,我主要使用 div 或 ap 元素以及clear:both;应用。
您喜欢哪些元素,或者这样做的“最佳实践”是什么?
I was wondering what the best element for clearing floated block-level elements would be?
For now, I mostly used a div or a p element with clear: both; applied.
What elements do you prefer, or what is something like the "best practice" for doing that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您真的只是想清除它们,那么无论元素如何最能描述您想要清除浮动的内容的语义。
如果您想让一个块扩展以包含其所有浮动内容,那么添加额外的元素(任何类型)是最肮脏的选择。有一大堆更好的方法来实现这种效果。我通常喜欢在容器上设置overflow:hidden,但最佳选项会因上下文而略有不同。
如果您确实想使用实际的(空)元素,那么 div 或 as span 是最好的 - 它们不带有额外的语义。
If you really just want to clear them, then whatever element best describes the semantics of the content that you want to clear the float.
If you want to cause a block to expand to contain all it's floating content, then adding an extra element (of any type) is the dirtiest option. There are a whole bunch of better ways to achieve the effect. I generally favour setting
overflow: hidden
on the container, but the best option varies a little with the context.If you really want to use an actual (empty) element, then either a div or as span is best — they don't come with extra semantics.
我有以下上下文:
其中 #sidebarWrap 绝对位于其父级的右上角,并且 #sidebarHandle 和 #sidebar 浮动以彼此相邻。
p.clear 清除浮动。
在这种情况下,是否还有更好的解决方案呢?
I have the following context:
Where #sidebarWrap is absolutely positioned to the top right of it's parent, and #sidebarHandle and #sidebar are floated to be next to each other.
the p.clear clears the floating.
In this case, would there be a better solution?