Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unexpected margin with very simple html

Tags:

html

css

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>
like image 498
Serhat Ozgel Avatar asked Nov 24 '08 22:11

Serhat Ozgel


People also ask

What does margin 10px 5px 15px 20px mean?

margin: 10px 5px 15px 20px; top margin is 10px. right margin is 5px. bottom margin is 15px. left margin is 20px.

How do you fix margins in HTML?

Adjusting the Margin Size of an HTML Element With CSS You can remove this margin by setting the top and left margin to zero. Like the padding and border, the sizes of specific sides of the margin can be set using margin-left , margin-right , margin-top , and margin-bottom .

How do I set text margin in HTML?

Margins on text is controlled by STYLE="margin: ", which is a style that can be used on all types of text section, including the BODY tag. Written like this, you specify all four margins in one take.

Why is my margin-right not working?

You cannot see the effect of margin-right because the 'width' property of div causes the margin of div to have more than 10px distance from the right wall of the body. Consequently, it causes the margin-right property not to have any visual effect.


3 Answers

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;
}
like image 195
mercator Avatar answered Oct 06 '22 01:10

mercator


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.

like image 27
strager Avatar answered Oct 06 '22 00:10

strager


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>
like image 38
Chris Lloyd Avatar answered Oct 06 '22 02:10

Chris Lloyd