gpt4 book ai didi

css - CSS float overflow 属性解释

转载 作者:行者123 更新时间:2023-11-28 11:34:35 27 4
gpt4 key购买 nike

我是 CSS 的新手。我开始探索 CSS float 属性。我有两个 <div> container 中的元素.两个divs具有相等的宽度和高度。

 <div class="container">
<div class="one">
</div>
<div class="two">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
</p>
</div>
</div>

上面的 CSS 是:

.one{
width: 200px;
height: 200px;
background-color: green;
border: 1px solid blue;
float: left;
}
.two{
width: 200px;
height: 200px;
background-color: red;
border: 1px solid black;
overflow:visible;
}

.container{
overflow:hidden;
}

从上面我能理解的是,由于 float 的 div 从正常流中删除并 float 到左侧,第二个 div 包含 <p> 中的文本。元素合并在 .one 下方分区而且由于两者具有相同的宽度和高度,所以我看不到内容。

但是当我设置 .two 的溢出属性时滚动我看到第二个 div 被放置在第一个 div 附近。这令人困惑。为什么会有这样的行为?请解释一下。链接到下面随附的代码笔以获取更多详细信息。 Link to Codepen

最佳答案

float CSS 属性指定应从正常流中获取元素。然而,非 float 元素仍然占据所有可用宽度,包括 float 元素。但是,如果您将 overflow: auto;hidden 设置为它,它将紧挨着 float 元素对齐。看下面的demo,应该解释清楚了。

.a1, .b1 {
background: rgba(0,0,0,.5); /*pure black with alpha*/
width: 50px;
height: 50px;
float: left;
}
.a2 {
background: lime;
}
.b2 {
background: lime;
overflow: auto; /*let the browser decide whether to clip or not*/
}
<div class="a1">hello</div>
<div class="a2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</div>

<br/>

<div class="b1">hello</div>
<div class="b2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</div>

关于css - CSS float overflow 属性解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30055141/

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