Why is adding or removing a border changing the way margins work?

后端 未结 1 1214
萌比男神i
萌比男神i 2021-01-28 07:32

If I have a container with an element inside that has a margin, that elements margin will not push it down within that container but instead push down the container itself. That

相关标签:
1条回答
  • 2021-01-28 07:40

    The specific answer to your question is collapsing margins and is part of the W3C's BOX MODEL specifications:


    Vertical margins may collapse between certain boxes:

    • 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. In the case of negative margins, the maximum of the absolute values of the negative adjoining margins is deducted from the maximum of the positive adjoining margins. If there are no positive margins, the absolute maximum of the negative adjoining margins is deducted from zero. Note. Adjoining boxes may be generated by elements that are not related as siblings or ancestors.
    • Vertical margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
    • Vertical 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 elements do not collapse (not even with their in-flow children).
    • 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.
      • Otherwise, either the element's parent is not taking part in the margin collapsing, or only the parent's bottom margin is involved. The position of the element's top border edge is the same as it would have been if the element had a non-zero bottom border.

    An element that has had clearance applied to it never collapses its top margin with its parent block's bottom margin.

    Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements. Margins of the root element's box do not collapse.


    So, when you add a border, you're doing exactly what the specification says, therefore you have no margin.

    There are many ways and fixes and hacks to solve this, but to me, the most direct and easy is as simple as to apply an overflow:auto; property to the div you want to clear the margin.

    Thus, in your CSS, you would only need to change it like this:

    .second-div
    {
        background-color: blue;
        min-height: 50px;
        overflow:auto;
    }
    

    I forked your fiddle with even more border so you can notice the effect and see how good it works

    0 讨论(0)
提交回复
热议问题