gpt4 book ai didi

html - 溢出 :hidden on div tag affects background color

转载 作者:搜寻专家 更新时间:2023-10-31 22:19:38 25 4
gpt4 key购买 nike

overflow:hidden 的定义指出:

溢出的内容完全隐藏,用户无法访问。

来自:http://quirksmode.org/css/css2/overflow.html

但我很好奇我的 js fiddle 中的这种行为: https://jsfiddle.net/gd62qmr3/2/

我注意到的一件事是,在将溢出设置为隐藏后它给边距添加了颜色?

<div style="background-color:green;overflow:hidden;">
<p>test</p>
</div>

我想知道为什么会出现这种行为?如果没有溢出,确切的 block 元素将具有绿色背景色,但放置溢出将为其边距提供背景色。

最佳答案

那是因为overflow: hidden影响了margin collapse。

p 元素默认有一些垂直边距。根据the spec ,它与父 div 的边距一起折叠:

In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.

Adjoining vertical margins collapse, except [... not relevant]

[... The] top margin of a box and top margin of its first in-flow child [are adjoining]

enter image description here

但是,overflow: hidden 阻止了:

Margins of elements that establish new block formatting contexts (such as floats and elements with overflow other than visible) do not collapse with their in-flow children.

enter image description here

关于html - 溢出 :hidden on div tag affects background color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33244754/

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