为什么 float 后面跟着clear

发布于 2024-10-02 11:25:21 字数 190 浏览 0 评论 0原文

向左浮动,然后向左清除是什么意思。或者向左浮动,然后向右清除?谁能帮助我理解这一点。我不是 CSS 专家,但需要了解一些我正在使用的 CSS。

#elem1{
    float:left;
    clear:left;
}

#elem2{
    float:left;
    clear:right;
}

What does it mean to float left, then clear left. Or float left, then clear right? Can anyone help me understand this. I'm not a CSS guy, but need to understand some of the CSS I'm working with.

#elem1{
    float:left;
    clear:left;
}

#elem2{
    float:left;
    clear:right;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

我还不会笑 2024-10-09 11:25:21

如果将两个元素浮动在一起,并且不应用清除,它们将在同一水平轴上对齐。

通过告诉浮动元素清除,它会清除其之前的所有浮动元素,然后将其自身浮动到设计者想要浮动的任何位置。它是否会换一条新线,取决于清除的方向。

请参阅此 jsFiddle 示例以获取可视化效果。前两个

不清除任何内容,而最后两个

设置为清除左右浮动。

If you float two elements together, they will align on the same horizontal axis if you don't apply clearing.

By telling a floating element to clear, it clears any floats that come before it, then floats itself wherever the designer intends to float it. It will go on a new line, or not, depending on the direction of the clear.

See this jsFiddle example for a visualization. The first two <div>s don't clear anything, while the last two <div>s are set to clear both left and right floats.

过度放纵 2024-10-09 11:25:21

clear 表示将元素定位在任何先前浮动元素的下方,以“清除线条”。

通常浮动元素会像这样堆积在同一行:

--- --- ---
| | | | | |
--- --- ---

清除它们之前的所有浮动元素将使它们排列如下:

---
| |
---
---
| |
---
---
| |
---

clear means to position the element below any previous floating elements, to "clear the line".

Usually floating elements pile up on the same line like this:

--- --- ---
| | | | | |
--- --- ---

Making them clear any previous floating elements will make them arrange like this:

---
| |
---
---
| |
---
---
| |
---
抱着落日 2024-10-09 11:25:21

这里可以更清楚地了解为什么需要清除。 http://www.quirksmode.org/css/clearing.html

Here is a clearer view of why a clear is needed. http://www.quirksmode.org/css/clearing.html

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