Outer margin collapsing (outer margin overlay)

Source: Internet
Author: User
Document directory
  • Margin collapsing
  • Margin collapsing is irrelevant to the Direction
  • Margin collapsing only occurs in BFC
  • Margin collapsing may span Elements
  • Margin collapsing cannot be folded with padding (padding)
  • Margin collapsing cannot match BFC

I have read the new book translated by Teacher Li songfeng, "CSS designer Guide (3rd edition" margin overlay part (http://www.ituring.com.cn/article/16969) is really a little anxious, this place is really some not written in place, there are also errors (for example, "Only vertical margin is superimposed, and horizontal margin is not superimposed ")

Margin collapsing

BFC (block formatting context) is adjacent to two block-level boxes. The bottom margin of the previous box is superimposed with the top margin of the next box, that is, the two are the largest, rather than the sum:

Run

<!doctype HTML>    #green {        margin:10px 10px 10px 10px    }    #blue {        margin:10px 10px 10px 10px    }    #red {        margin:10px 10px 10px 10px    }</style>Margin collapsing is irrelevant to the Direction

In the CSS designer Guide (version 3rd), it is wrong to say that "only vertical margins are superimposed, while horizontal margins are not superimposed, whether the two boxes are overlapped depends entirely on whether they are located in two adjacent boxes of the same BFC, regardless of the horizontal vertical. In the following example, the writing-mode of the body is changed, so the stack is in the horizontal direction (only visible to ie)

Run

<!doctype HTML>    #green {        margin:10px 10px 10px 10px    }    #blue {        margin:10px 10px 10px 10px    }    #red {        margin:10px 10px 10px 10px    }
body {
writing-mode:tb-rl;
}</style>Margin collapsing only occurs in BFC
Margin collapsing does not occur in IFC

When the display attribute of an element is set to inline-block, they are all in the IFC, so they will not overlap either horizontally or vertically:

Run

<!doctype HTML>    #green {        margin:10px 10px 10px 10px;
display:inline-block; } #blue { margin:10px 10px 10px 10px;
display:inline-block; } #red { margin:10px 10px 10px 10px;
display:inline-block; }
</style>Margin collapsing does not occur in floats

When the float attribute of elements is set to left, they are not in the normal stream, and are not in the BFC. Similarly, they will not overlap in both the horizontal and vertical directions:

Run

<!doctype HTML>    #green {        margin:10px 10px 10px 10px;
float:left; } #blue { margin:10px 10px 10px 10px;
float:left; } #red { margin:10px 10px 10px 10px;
clear:left;
float:left; }
</style><div id="red" style="background:pink;height:100px;width:100px;"></div></body>
Margin collapsing may span Elements

When a container has neither border nor padding, the margin of its first sub-box can also overlap with the margin of its previous container:

Run

<!doctype HTML>    #green {        margin:10px 10px 10px 10px;
} #blue { margin:5px 10px 10px 10px;
} #red { margin:10px 10px 10px 10px;
}
</style> <div id="red" style="background:pink;height:100px;width:100px;"></div>
</div></body>
Margin collapsing cannot be folded with padding (padding)

When padding (padding) exists, the previous section does not occur:

Run

<!doctype HTML>    #green {        margin:10px 10px 10px 10px;
} #blue { margin:10px 10px 10px 10px;
padding:10px 10px 10px 10px;
} #red { margin:10px 10px 10px 10px;
}
</style> <div id="red" style="background:pink;height:100px;width:100px;"></div>
</div></body>

 

Margin collapsing cannot match BFC

When a container creates a new BFC inside it, cross-element folding does not occur.

Run

<!doctype HTML>    #green {        margin:10px 10px 10px 10px;
} #blue { margin:10px 10px 10px 10px;
overflow:hidden;
} #red { margin:10px 10px 10px 10px;
}
</style> <div id="red" style="background:pink;height:100px;width:100px;"></div>
</div></body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.