为什么这个块 div 会影响两个浮动 div?

发布于 2024-10-31 03:42:01 字数 750 浏览 3 评论 0原文

假设我的页面中有三个

<div id="left" class="test" style="float:left;"></div>
<div id="right" class="test" style="float:right;"></div>
<div id="footer">footer</div>

使用这个 css:

.test{  background:black;height:200px;width:200px;}
#footer{    background:yellow;margin:20px 0 0 0;}

我想要的是:

  • 让“#left”向左浮动,
  • 让“#right”向右浮动,
  • 什么都不改变对于“#footer”,只需将其设置为 margin: 20px;

结果如下:

enter image description here

但我想知道为什么浮动的 div 也有与 #footer 相同的边距。它们是浮动的,因此它们独立于其他元素,为什么 #footer 会影响它们?

Suppose I have three <div>s in my page,:

<div id="left" class="test" style="float:left;"></div>
<div id="right" class="test" style="float:right;"></div>
<div id="footer">footer</div>

with this css:

.test{  background:black;height:200px;width:200px;}
#footer{    background:yellow;margin:20px 0 0 0;}

What I want is:

  • let the "#left" float to left
  • let the "#right" float to right
  • change nothing about the "#footer", just set it to margin: 20px;

The result is below:

enter image description here

But I wonder why the floated divs also have the same margin as the #footer. They are floated, so they're independent of the other elements, why would the #footer could affect them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笛声青案梦长安 2024-11-07 03:42:01

以及页脚上的 clear:both ,只需在元素周围添加一个容器“包装器”div 即可阻止这种情况发生 - 示例

实际上在页脚上添加 clear: Both; 不会让浮动和页脚之间有 20px 的间隙或者,您实际上需要向浮动添加 20px 底部边距 - 原因都链接到 许可或非许可及其与 边距折叠

为什么?

您说您想知道为什么会发生这种情况,在您的 OP 场景中,这是因为边距崩溃

在原始示例中没有涉及间隙,所以是的,浮动被删除,因此页脚边距仍然相邻,因此与 body 元素折叠,因此 body element 是获得边距的元素,然后因为浮动实际上仍然在 body 内部,所以它们也获得了边距。

正如我上面提到的,创建一个包装器 div 来“包含”浮动会阻止这种情况发生,因为折叠规则也是如此。但是,您选择包含浮动,或者使用溢出:隐藏,或者通过浮动“包装器”来停止此交互,因为..来自折叠边距的部分:

元素的垂直边距
建立新的块格式
上下文(例如浮动和元素
与“溢出”而不是“可见”)
不要随着他们的流入而崩溃
孩子们。

你会看到, float 和“overflow other notvisible”这两个属性都是“包含浮动子项”的方法 - 实际上它们正在建立一个新的块格式化上下文,但简单地说,大多数人都知道它是“包含浮动”; )

现在一旦你有了这个,就修复了你的第一个位,但是如果你决定在页脚上引入 clear:both ,现代浏览器将不会在之间添加 20px 的边距浮动和页脚..这实际上是正确的..来自 clear 属性的部分(我的粗体):

然后间隙量设置为
较大者:

  1. 放置块边界边缘所需的量甚至
    最低的底部外边缘
    待清除的浮动。

  2. 将块的顶部边框边缘放置在
    它的假设位置。

为了将页脚的上边缘放置在浮动下方(在您的示例中),浏览器必须引入 200 像素的间隙,这远远超过 20 像素,因此它遵循规则 1。如果您的上边距页脚上的页边距为 220px,边距将大于所需的任何间隙,因此它将遵循规则 2。

因此,如果您确实希望页脚位于浮动体下方 20px,无论浮动体的高度是多少,您都可以将20px 作为两个浮动的底部边距,因此它[页脚]将通过清除规则 1 清除具有所需间隙/边距的浮动,无论哪个浮动最长。

PS:不要在 IE7 或更低版本中测试以上内容 - 我希望它不会太无聊;)

as well as clear:both on the footer, just adding a container "wrapper" div around the the elements will stop this happening - example

actually adding clear: both; on the footer won't give you a 20px gap between the floats and the footer either, you would actually need to add the 20px bottom margin to the floats - the reasons are all linked.. to clearance or non clearance and it's interaction with Collapsing Margins

Why?

You said you wanted to know why this is happening, in your OP scenario it's because of Collapsing Margins.

You have no clearance involved in the original example, so yes the floats are removed, So the footer margin is still adjoining, therefore collapsing with, the body element, so the body element is the one getting the margin, and then because the floats are still actually inside the body they get the margin too.

As I mentioned above creating a wrapper div to "contain" the floats stops this happening because the rules of collapsing too. However you choose to contain the floats, either with overflow:hidden, or by floating the "wrapper" stops this interaction because .. from the section on collapsing margins:

Vertical 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.

you see that both of the properties, float and 'overflow other than visible' are the means to "contain floated children" - actually they're establishing a new block formatting context, but in easy speak most know it as "containing floats" ;)

Now once you have that, that fixes your first bit but then if you decide to introduce clear:both on the footer, the modern browsers will not put a 20px margin between the floats and the footer.. this is actually correct.. from the section on the clear property (my bold):

Then the amount of clearance is set to
the greater of:

  1. The amount necessary to place the border edge of the block even with
    the bottom outer edge of the lowest
    float that is to be cleared.

  2. The amount necessary to place the top border edge of the block at
    its hypothetical position.

In order to place the top edge of the footer below the floats (in your example) the browser has to introduce 200px of clearance, which is far more than 20px so it follows rule 1. If your top margin on the footer was 220px, the margin would be greater than the any clearance needed, so it would follow rule 2.

So, if you did actually want the footer to be 20px below the floats no matter what their heights are, you would put the 20px as a bottom margin onto the two floats, so it [the footer] would clear, via clearance rule 1, the floats with the required gap/margin, no matter which was float the longest.

PS: Don't test the above in IE7 or below - and I hope it wasn't too boring ;)

長街聽風 2024-11-07 03:42:01

clear: Both 添加到 #footer CSS。这应该使页脚呈现在浮动 div 下方并具有所需的边距。

Add a clear: both to the #footer CSS. That should make the footer render below the floating divs with the margin you want.

小苏打饼 2024-11-07 03:42:01

试试这个,这可能会解决你的问题:

<div id="right" class="test" style="float:right;"></div>
<div id="left" class="test"></div>
<div id="footer">footer</div>

CSS 保持不变。

Try this and this may solve your problem:

<div id="right" class="test" style="float:right;"></div>
<div id="left" class="test"></div>
<div id="footer">footer</div>

CSS remaining unchanged.

剧终人散尽 2024-11-07 03:42:01

我做了一个测试,然后发现这个页面有两个框,右侧浮动的框受到左侧框(在它之后)的影响:http://jsfiddle.net/4r75s/

防止父 div 在仅包含浮动内容时崩溃的溢出技巧似乎在这里起作用,即将溢出设置为隐藏、自动或滚动。我将它们包装在一个包含 div 中来执行此操作并且它有效: http://jsfiddle.net/4r75s/1/

#container {
    overflow: hidden;
}

I made a test before finding this page that has two boxes, with the right-floated one being affected by the left block one (which comes after it) here: http://jsfiddle.net/4r75s/

The overflow trick that prevents parent divs collapsing when they only contain floated content seems to work here, that is setting overflow to hidden, auto or scroll. I wrapped them in a containing div to do it and it works: http://jsfiddle.net/4r75s/1/

#container {
    overflow: hidden;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文