gpt4 book ai didi

html - CSS float 逻辑

转载 作者:太空宇宙 更新时间:2023-11-04 14:33:31 25 4
gpt4 key购买 nike

我创建了 25 个随机宽度和高度的框,其中 width == height(如图所示)

$(document).ready(function(e) {
for (var count = 0; count < 5; count++) {
for (var iter = 0; iter < 5; iter++) {
$(".content").append("<div id='" + count + "_" + iter + "' class='box'><p>" + count + "_" + iter + "</p></div>");
$(".content div:last").width(Math.round((Math.random() * 100) + 50));
$(".content div:last").height($(".content div:last").width());
}
}
});
.box {
background: #FF0004;
margin: 10px;
float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
<div class="content">
</div>
</body>

我试图更好地理解 float 属性逻辑以及 float 元素相互“堆叠”时定位背后的原因。例如(从 fiddle 中截取的屏幕截图):

enter image description here

是什么让3_3在屏幕变大的时候重新定位到如下图的位置? enter image description here

根据 W3Schools:

If you place several floating elements after each other, they will float next to each other if there is room.

所以问题是,“room”是什么意思,控制 float 元素位置的逻辑是什么?

这个 SO 帖子的答案:CSS Float explanation似乎相关,但似乎无法解释为什么 float 元素会停在某个位置。

最佳答案

这是与您的问题最相关的链接答案部分:

When you float a block element, you are telling the browser to position it next to the previous floated object, so long as the container is wide enough (otherwise it will drop below the previous object).

正如作者所提到的,这是一种粗略的简化。 Section 9.5.1规范的一部分列出了所有精确的规则,但我不打算在这里引用全部内容,因为它是一本非常的长篇读物,并且只有某些点在这里是相关的。当我逐步了解您的 fiddle 中发生的事情时,我会引用相关要点。

比较您的两个屏幕截图。注意位置发生变化的两个框 3_2 和 3_3,以及它们周围的框 2_4、3_0 和 3_1。

之前

Before

之后

After

当屏幕变大时,您为 3_2 腾出空间,使其从 2_4 旁边的原始位置向上移动到 3_1 旁边:

  1. A left-floating box that has another left-floating box to its left may not have its right outer edge to the right of its containing block's right edge. (Loosely: a left float may not stick out at the right edge, unless it is already as far to the left as possible.) An analogous rule holds for right-floating elements.
  1. A floating box must be placed as high as possible.
  1. A left-floating box must be put as far to the left as possible, a right-floating box as far to the right as possible. A higher position is preferred over one that is further to the left/right.

这又为下一个 float 框腾出空间,以尽可能向上和向左占据空间,遵循与上述相同的规则。结果,3_3 向上 float ,在 3_2 附近停止,然后它沿水平轴尽可能向左浮动,在 2_4 附近停止。请注意,尽管看起来 3_3 应该能够容纳在 2_4 和 3_2 之间,但事实并非如此,因为必须考虑边距(这就是上面“外边缘”的意思)。

此时,以下元素适用,除了上面的第 8 项和第 9 项:

  1. If the current box is left-floating, and there are any left-floating boxes generated by elements earlier in the source document, then for each such earlier box, either the left outer edge of the current box must be to the right of the right outer edge of the earlier box, or its top must be lower than the bottom of the earlier box. Analogous rules hold for right-floating boxes.
  1. The outer top of a floating box may not be higher than the outer top of any block or floated box generated by an element earlier in the source document.

由于 3_3 太大,它从第一行 float 框创建了一个明显的向下突出。这有效地增加了第一行的高度。 3_3 之后的所有其他 float 元素必须保留在自己的行中,并且第二行 float 元素不得与 3_3 的底部边距边缘相交。这主要由元素 #5 管理。

关于html - CSS float 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30858717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com