gpt4 book ai didi

html -

在 float 中的行为

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

我有以下 HTML:

<div id="test">       
<p>
Floating behaves differently with paragraphs. Floating behaves differently with
paragraphs. Floating behaves differently with paragraphs.
</p>
</div>

使用这个 CSS:

#test{
float:left;
background-color: #222;
margin-left: 4%;
width: 300px;
padding-top: 15px;
padding-right: 30px;
padding-left: 30px;
text-align: justify;
color: #FFF;
}

您可以在 http://jsfiddle.net/fM4bE/ 看到p/p 创建的test div 底部有一个空格。如果我们删除 float:left 这个空间就消失了,就好像它不是任何/p 一样......谁能向我解释为什么?

谢谢

最佳答案

如果没有在 div 上应用 float:left,您会看到 collapsing margins ,其中 div 和 p 合并的底部边距:

Parent and first/last child If there is no border, padding, inline content, or clearance to separate the margin-top of a block with the margin-top of its first child block, or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block with the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.

但是,float:left 创建了一个新的 block formatting context ,类似于将 overflow:auto 添加到 div 以代替 float:left 时的效果。这使得边距不会塌陷。

Block formatting contexts are important for the positioning (see float) and clearing (see clear) of floats. The rules for positioning and clearing of floats apply only to things within the same block formatting context. Floats do not affect the layout of things in other block formatting contexts, and clear only clears past floats in the same block formatting context.

另请参阅:http://www.w3.org/TR/CSS21/visuren.html#block-formatting

关于html - <p> 在 float 中的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23744739/

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