简体   繁体   English

CSS float的异常行为

[英]Unexpected behavior of css float

Here is my jsFiddle 这是我的jsFiddle

I just have 3 div s. 我只有3 div s。 The 2nd div is floated to the right, and 3rd div appears just below the 2nd. 第2个div浮动在右侧,第3个div出现在第2个div正下方。

In the 3rd div , I am setting margin-top property, and this property does not have any effect on the layout. 在3 div ,我设置margin-top属性,并且此属性对布局没有任何影响。

Question : Can someone explain me understanding this behavior of float? 问题 :有人可以解释我对浮动的这种行为的理解吗?

HTML HTML

<div class="header">
</div>
<div class="sidebar">
</div>
<div class="footer">
</div>

CSS CSS

.header {
    width: 100%;
    height: 80px;
    background-color: #abcdef;
}

.sidebar {
    margin-top: 15px;
    float: right;
    width: 200px;
    height: 200px;
    background-color: #abcdef;
    display: block;
}

.footer {
    clear: both;
    margin-top: 20px;
    width: 100%;
    height: 60px;
    background-color: #000000;
}

This is not unexpected at all. 这一点也不意外。 The .sidebar is removed from regular flow layout by its float property, as such it doesn't take up any space anymore. .sidebar通过其float属性从常规流布局中删除,因此它不再占用任何空间。 The .footer has a clear rule, so it is forced underneath any floats, but that automatically puts it 215px (margin+height of the sidebar) behind the last element that is part of the flow layout. .footer有一个clear规则,因此将其强制放置在所有浮点下方,但这会自动将其放到215px(边栏的边距+高度)旁边,该元素流布局的一部分。 As such its margin requirement of 20px is completely satisfied, and it appears at its logical current position. 因此,完全满足其20px的保证金要求,并显示在其逻辑当前位置。 You can verify this by setting the top margin to 220px instead, it will appear 5px (220-215) below the sidebar. 您可以通过将顶部边距设置为220px来验证这一点,它会在边栏下方显示5px(220-215)。

You can easily achieve the effect you desire by putting margin-bottom:20px on the sidebar since it will then be required to keep that distance to the footer, pushing it down. 您可以通过在边栏上放置margin-bottom:20px轻松实现所需的效果,因为随后需要与页脚保持该距离并将其向下推。

The issue is related to the clear rule. 问题与明确规则有关。

W3C - An element that has had clearance applied to it never collapses its top margin with its parent block's bottom margin. W3C-对其应用了净空的元素,其父边界的下边界永远不会塌陷其上边界。

Baiscally, if you want to use clear, the general rule is to add an element between the two floated divs to ensure you can correctly space them. Baiscally,如果要使用clear,通常的规则是在两个浮动div之间添加一个元素,以确保可以正确地隔开它们。

This is caused by the fact that floated elements aren't really there with respect to margin calculations. 这是由于以下事实造成的:就保证金计算而言,浮动元素实际上并不存在。 Your .footer is below whatever unfloated elements are above it, (with a margin of 20px). 您的.footer位于其上方任何未浮动元素的下方(边距为20px)。 This issue is caused because margins with respect to floats are calculated relative to other floats, (not all other elements). 出现此问题的原因是,相对于其他浮动(不是所有其他元素)计算相对于浮动的边距。 So to get the desired effect add a margin-bottom element to .sidebar, have a meaningless float added to the .footer, or add a 因此,要获得所需的效果,请在.sidebar中添加一个margin-bottom元素,在.footer中添加一个无意义的float,或者添加一个

<div style="clear:both"></div>

between the .footer and .sidebar 在.footer和.sidebar之间

The top margin of the footer div is being collapsed, http://www.w3.org/TR/CSS21/box.html#collapsing-margins 页脚div的上边距已折叠, http://www.w3.org/TR/CSS21/box.html#collapsing-margins

If you add margin-bottom to the sidebar instead of the top of the footer it will work. 如果将边距底部添加到侧边栏而不是页脚的顶部,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM