Home > Article > Web Front-end > Css collapsed margin (collapsed margin) brief analysis_html/css_WEB-ITnose
Css collapses margin
a. Let’s first look at the w3c document’s definition of margin collapse:
In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.
Translation: in In CSS, the margins of adjacent boxes (which may or may not be sibling elements) can be combined into one margin. This merging of margins is called Collapse, and this merged margin is called Collapsed margins.
For example:
<style type="text/css">.testBFC{ width:100px; height:100px; background-color:green;}.testBFC div{ width:30px; height:30px; background-color:pink; }.testBFC div:first-child{ margin-bottom:10px;}.testBFC div:last-child{ margin-top:10px;}</style><div class="testBFC"> <div>div1</div> <div>div2</div></div>
The running result is as shown below. We can see from the picture that the margin-bottom of div1 and the margin-top of div2 are merged. As a result, there is only 10px between div1 and div2. We originally wanted them to be 20px apart. How to achieve this? You can add: display: inline-block to the class of div1. This involves how to solve or avoid the problem of margin collapse.
Let’s take a look at the specific notes in the w3c document:
Note the above rules imply that:
1. Floating boxes will not collapse between other boxes. (Folding will not occur even between a floating box and its normal flow child elements). For example, in the following example, the outer edge will collapse between the parent element and the child element. In this case, it can be set by the parent element or the child element. If it becomes floating, it will display normally.
<head> <style type="text/css"> *{ margin:0; padding:0; } .testBFC{ width:100px; height:100px; background-color:green; margin-top:10px; } .testBFC div{ width:30px; height:30px; background-color:pink; } .testBFC div:first-child{ margin-top:10px; } </style></head><body><div class="testBFC"> <div>div1</div></div></body>
2. Create a new block formatting context (block-level formatting context) for the element's margins, so that it will not conflict with its normal Child elements in the flow are collapsed. As in the above example, you can add an attribute overflow:hidden
3 to the outer div. Absolutely positioned boxes will not collapse. In the above example, you can add attributes to the parent element or child element: position:absolute or fixed.
4. In a normal document flow, the bottom margin of the block-level element is equal to the one behind it. The top margin of sibling elements collapses unless you set clearance. See example at top of article.
5. The top margin of a block-level element will collapse with the top margin of its child element of the first block-level element. If this block-level element does not set top border, no top padding, the child The element does not have clearance. Example: This example is already in tip 1.
6. The bottom margin of a block-level element may collapse with the bottom margin of its last block-level child element. If this block-level element does not set bottom padding, bottom border and height:auto , min-height:0
7. Such a box will collapse the margins, min-height:0, and there is no top or bottom borders or top or bottom padding set, and height:0 or auto and it does not Contains the line box and collapses the margins of all its child elements.
For detailed information, please refer to the w3c document: http://www.w3.org/TR/CSS2/box.html#collapsing-margins