gpt4 book ai didi

html - Div 不会填充空白区域

转载 作者:太空宇宙 更新时间:2023-11-03 20:28:51 25 4
gpt4 key购买 nike

我相信这是 CSS 的默认行为,但我不确定为什么,如果有人能解释一下,那就太好了。

内容有 3 个 div 标签(A、B、C)。 A 和 B 以 100% 的宽度(60%+40%) float ,但它们的高度不同。 A 为 300px,B 为 400px 高度。 C 的宽度为 40%,它也向左浮动。

.content {
width: 400px;
height: 800px;
border: solid 1px;
}

.column-a {
width: 60%;
height: 300px;
background: green;
float: left;
}

.column-b {
width: 40%;
height: 400px;
background: red;
float: left;
}

.sub-columns {
width: 40%;
height: 200px;
background: orange;
float: left;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="content">
<div class="column-a">A</div>
<div class="column-b">B</div>
<div class="sub-columns">C</div>
</div>
</body>
</html>

我的想法是 C 放在 A 的正下方,因为有空间,但浏览器似乎不会把它放在那里,因为 B div 高于 A,而 C 出现在 B div 下面。图片:

enter image description here

为什么会这样,C不应该放在A下面吗?

最佳答案

至于为什么会发生这种情况 - 这是因为 float 元素的行为是指定的。

https://www.w3.org/TR/CSS21/visuren.html#floats有一个标题为“这是管理 float 行为的精确规则”的列表,该列表中的第二项是:

  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.

由于 B 旁边没有 C 的空间,因此“当前框的左外边缘必须在先前框的右外边缘的右侧”部分要求无法满足 - 所以“它的顶部必须低于前面框的底部”变得适用。


请记住,float 基本上是 CSS“石器时代”的一个属性。它甚至从未打算用于创建“列布局”,而只是让文本 float 在图像周围,诸如此类的简单内容。当然,我们将它用于专栏很长一段时间 - 但那是因为我们没有其他真正适合做这项工作的东西,而不是因为它是开始这项工作的合适工具。但当时的浏览器还不那么复杂,这可能是它以这种方式指定的主要原因 - 相对容易实现(但 Internet Explorer 设法搞乱了 的 float /清除>long 时间。)如果当时有人向浏览器开发人员抛出 flexbox 或网格规范,他们可能会消失,你一定是在开玩笑,这种复杂性是无法通过资源和计算来管理的我们在这里拥有强大的力量……如果那是你的需求,我们不会首先开始实现这个“CSS”的东西,你们这些疯子 ...;-)

关于html - Div 不会填充空白区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47115902/

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