CSS 中清除浮动的最佳元素?

发布于 2024-08-04 07:09:00 字数 106 浏览 2 评论 0原文

我想知道清除浮动块级元素的最佳元素是什么?

目前,我主要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荆棘i 2024-08-11 07:09:00

如果您真的只是想清除它们,那么无论元素如何最能描述您想要清除浮动的内容的语义。

如果您想让一个块扩展以包含其所有浮动内容,那么添加额外的元素(任何类型)是最肮脏的选择。有一大堆更好的方法来实现这种效果。我通常喜欢在容器上设置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.

小矜持 2024-08-11 07:09:00

我有以下上下文:

<div id="sidebarWrap">
    <div id="sidebarHandle">
        <a href="#"></a>
    </div>
    <div id="sidebar">
        <h2>Category</h2>                           
    </div>
    <p class="clear"></p>
</div>

其中 #sidebarWrap 绝对位于其父级的右上角,并且 #sidebarHandle 和 #sidebar 浮动以彼此相邻。
p.clear 清除浮动。

在这种情况下,是否还有更好的解决方案呢?

I have the following context:

<div id="sidebarWrap">
    <div id="sidebarHandle">
        <a href="#"></a>
    </div>
    <div id="sidebar">
        <h2>Category</h2>                           
    </div>
    <p class="clear"></p>
</div>

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文