gpt4 book ai didi

html - 为什么不顶: 0 work on absolutely-positioned elements relative to body?

转载 作者:技术小花猫 更新时间:2023-10-29 12:10:13 27 4
gpt4 key购买 nike

您可以在下面的代码中看到,h1 将主体向下推,而绝对定位 block .absolute 不会粘在顶部。但您也可以看到同一 block 粘在其父 .wrapper 的顶部。为什么?

我不是在问如何做到这一点;我知道如何,例如padding 而不是 margin 到 h1,或者 clearfix 到 parent 等等。

我只对一件事感兴趣:为什么 h1 的边距会压低 body,但不会压低 .wrapper

body {
position: relative;
margin: 0;
padding: 0;
overflow: hidden;
background-color: silver;
}

.absolute {
position: absolute;
top: 0;
left: 0;
width: 50px;
height: 50px;
background-color: darkblue;
}

.wrapper {
position: relative;
overflow:hidden;
width: 50%;
height: 200px;
overflow: hidden;
background-color: yellow;
}

.wrapper > .absolute {
background-color: darkcyan;
}
<div class="absolute"></div>
<h1>Some title</h1>

<div class="wrapper">
<div class="absolute"></div>
<h1>Some title</h1>
</div>

好的,我会尽量说得更清楚。如果你点击下面的链接,你可以看到我的 JSFiddle。 body 标签中有background-color: silver。当我查看代码检查器时,我发现由于 h1 边距,正文标签开始略低。但是 background-color 从顶部开始。这意味着代码检查员在骗我,body 从顶部开始。但是,为什么作为 body 的直接子元素的绝对定位元素不从顶部开始呢?

JSFiddle

最佳答案

如前所述,顶级绝对定位元素的包含 block 是 body,因为 body 是相对定位的。当 h1 的边距 collapses对于 body,这会导致边距影响 body,进而影响它包含的绝对定位元素。相反,如果 body 不是 相对定位,绝对定位的元素将锚定到初始包含 block ,并粘在视口(viewport)的顶部,不受任何有效边距的影响在 body 上(因为 body 不再是它的包含 block )。

至于为什么银色背景会超出 body 元素,那是 by design :

3.11.1. The Canvas Background and the Root Element

The background of the root element becomes the background of the canvas and its background painting area extends to cover the entire canvas. However, any images are sized and positioned relative to the root element as if they were painted for that element alone. (In other words, the background positioning area is determined as for the root element.) The root element does not paint this background again, i.e., the used value of its background is transparent.

3.11.2. The Canvas Background and the HTML <body> Element

For documents whose root element is an HTML HTML element or an XHTML html element: if the computed value of ‘background-image’ on the root element is ‘none’ and its ‘background-color’ is ‘transparent’, user agents must instead propagate the computed values of the background properties from that element's first HTML BODY or XHTML body child element. The used values of that BODY element's background properties are their initial values, and the propagated values are treated as if they were specified on the root element. It is recommended that authors of HTML documents specify the canvas background for the BODY element rather than the HTML element.

根元素的默认背景色始终是透明,因此在html 元素上设置背景色可防止银色背景渗出body 元素(你会发现 inspector 实际上没有骗你):

html {
background-color: white;
}

body {
position: relative;
margin: 0;
padding: 0;
overflow: hidden;
background-color: silver;
}

.absolute {
position: absolute;
top: 0;
left: 0;
width: 50px;
height: 50px;
background-color: darkblue;
}

.wrapper {
position: relative;
overflow:hidden;
width: 50%;
height: 200px;
overflow: hidden;
background-color: yellow;
}

.wrapper > .absolute {
background-color: darkcyan;
}
<div class="absolute"></div>
<h1>Some title</h1>

<div class="wrapper">
<div class="absolute"></div>
<h1>Some title</h1>
</div>

关于html - 为什么不顶: 0 work on absolutely-positioned elements relative to body?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33594788/

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