unexpected margin with very simple html

2020-04-07 18:57发布

I have a very simple html. The red div is inside the blue div and has a 10 px top margin. On non-ie browsers, the blue box is 10 px apart from the top of viewport and the red div is at the very top of the blue div. What I expect is the ie behavior: red div must be 10 px apart from the top of the blue div. Why does non-ie browsers render like this? (I suppose the wrong behavior is the IE's but why?)

And, what is the correct way to do this?

why blank? http://img92.imageshack.us/img92/7662/blankmr7.jpg

<html>
<head>
<style>
body { margin:0; padding:0; }
.outer
{
    background-color: #00f;
    height: 50px;    
}
.inner
{
    height: 20px;
    width: 20px;
    background-color: #f00;
    margin: 10px 0 0 10px;
}
</style>
</head>
<body>
<div class="outer">
    <div class="inner">
    </div>
</div>
</body>
</html>

标签: html css
5条回答
我想做一个坏孩纸
2楼-- · 2020-04-07 19:34

As much as strager's answer already explains about as much as you need to know as to why it happens – namely that it happens the way it does in browsers other than IE because the specs say so – I think he picked the wrong quote from the section of the CSS 2.1 specification about collapsing margins.

The point he quoted explains how margins can collapse, not how they can "move" to a parent element.

This is rather what explains it:

  • If the top and bottom margins of a box are adjoining, then it is possible for margins to collapse through it. In this case, the position of the element depends on its relationship with the other elements whose margins are being collapsed.
    • If the element's margins are collapsed with its parent's top margin, the top border edge of the box is defined to be the same as the parent's.

Or, in slightly more human-readable form in the Mozilla developer documentation:

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.

As for how to fix it, I'd probably go for the overflow: auto solution Chris Lloyd suggested (as much as that may have side-effects).

But then that really depends on what exactly the rest of your code looks like. In this simple example you could easily just change the margin on the child element to a padding on the parent element.

Or you could float the child element, or absolutely position it...

Or how about an inverse clearfix if you want to get really fancy:

.outer:before {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}
查看更多
We Are One
3楼-- · 2020-04-07 19:37

There is a pretty fitting answer to this question: overflow: auto;

<html>
<head>
<style>
body { margin:0; padding:0; }
.outer
{
    background-color: #00f;
    height: 50px;
    overflow: auto;
}
.inner
{
    height: 20px;
    width: 20px;
    background-color: #f00;
    margin: 10px 0 0 10px;
}
</style>
</head>
<body>
<div class="outer">
    <div class="inner">
    </div>
</div>
</body>
</html>
查看更多
叼着烟拽天下
4楼-- · 2020-04-07 19:40

The margins are being merged. The output produced by IE is probably incorrect.

In the specifications (which are down for me at the moment):

Two or more adjoining vertical margins of block boxes in the normal flow collapse. The resulting margin width is the maximum of the adjoining margin widths.

You can use borders instead of margins, with border-color set to transparent.

查看更多
神经病院院长
5楼-- · 2020-04-07 19:45

Ok, solution without overflow auto:

<html>
<head>
<style>
body { margin:0; padding:0; }
.outer
{
    background-color: #00f;
    height: 50px;
    border: 1px solid transparent;
}
.inner
{
    height: 20px;
    width: 20px;
    background-color: #f00;
    margin: 10px 0 0 10px;
    padding: 0;
}
</style>
</head>
<body>
<div class="outer">
    <div class="inner">
    </div>
</div>
</body>
</html>

The inner element is wanting something to push against, and providing a boder (or forcing the browser to consider the overflow) does this.

查看更多
▲ chillily
6楼-- · 2020-04-07 19:51

Could it be IE sees the DOM as div.inner having div.outer as it's parent node(and calculates offset from it),
and that other browsers instead has both of them answering to the body element?

查看更多
登录 后发表回答