gpt4 book ai didi

css - float 元素的零高度边缘会被视为边缘吗?

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

根据 CSS 规范:

A floated box is shifted to the left or right until its outer edge touches the containing block edge or the outer edge of another float. If there is a line box, the outer top of the floated box is aligned with the top of the current line box.

当我写这个demo时,我发现当高度为零时, float 总是会折叠。但是我没有在 CSS Specification 上找到任何关于这个的声明。我的问题是为什么它会这样?为什么高度为零的边不被视为边?

.container{
height:500px;
width:800px;
background:pink;
}

.f{
float:left;
width:100px;
/* height:50px; */
}

.r{
position:relative;
}

.a{
position:absolute;
top:0;
left:0;
}
<div class='container'>
<div class='f'>
<div class='r'>
<div class='a'>ITEM1</div>
</div>
</div>
<div class='f'>
<div class='r'>
<div class='a'>ITEM2</div>
</div>
</div>
</div>

最佳答案

仅仅因为它的高度为 0,所以没有边,包含 block 的边将被考虑,逻辑上你会溢出。

添加一些动画以更好地查看效果:

.container{
height:500px;
width:800px;
background:pink;
}

.f{
float:left;
width:100px;
height:50px;
background:red;
}

.r{
position:relative;
}

.a{
position:absolute;
top:0;
left:0;
}

@keyframes change {
to {
height:0;
}
}
<div class='container'>
<div class='f' style="animation:change 3s linear forwards">
<div class='r'>
<div class='a'>ITEM1</div>
</div>
</div>
<div class='f' >
<div class='r'>
<div class='a'>ITEM2</div>
</div>
</div>
</div>

当达到 0 时,我们在第一个 float 元素上不再有边缘,因此第二个 float 元素将移动以接触包含 block 的边缘。


拥有边缘的唯一方法是确保至少有少量的高度,即使它是边框、填充等。

.container{
height:500px;
width:800px;
background:pink;
}

.f{
float:left;
width:100px;
height:50px;
background:red;
}

.r{
position:relative;
}

.a{
position:absolute;
top:0;
left:0;
}

@keyframes change {
to {
height:0;
}
}
<div class='container'>
<div class='f' style="animation:change 3s linear forwards;border-top:0.2px solid">
<div class='r'>
<div class='a'>ITEM1</div>
</div>
</div>
<div class='f' >
<div class='r'>
<div class='a'>ITEM2</div>
</div>
</div>
</div>

关于css - float 元素的零高度边缘会被视为边缘吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55833808/

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