Home  >  Article  >  Web Front-end  >  Css collapsed margin (collapsed margin) brief analysis_html/css_WEB-ITnose

Css collapsed margin (collapsed margin) brief analysis_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:43:081251browse

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:

  • Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
  • Margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.
  • Margins of absolutely positioned boxes do not collapse (not even with their in-flow children).
  • Margins of inline- block boxes do not collapse (not even with their in-flow children).
  • The bottom margin of an in-flow block-level element always collapses with the top margin of its next in-flow block-level sibling , unless that sibling has clearance.
  • The top margin of an in-flow block element collapses with its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance.
  • The bottom margin of an in-flow block box with a 'height' of 'auto' and a 'min-height' of zero collapses with its last in-flow block-level child's bottom margin if the box has no bottom padding and no bottom border and the child's bottom margin does not collapse with a top margin that has clearance.
  • A box's own margins collapse if the 'min-height' property is zero , and it has neither top or bottom borders nor top or bottom padding, and it has a 'height' of either 0 or 'auto', and it does not contain a line box, and all of its in-flow children's margins (if any) collapse.
  • 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

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn