Margin goes outside div when border is removed

前端 未结 4 1856
后悔当初
后悔当初 2021-01-21 13:43

Derived from an actual problem with borders and margin on my site I have made this test example which I think acts a little strange:




        
4条回答
  •  鱼传尺愫
    2021-01-21 13:53

    The margin on the outer2 element is calculated from the bottom of the element above it with no top margin applied to the outer2 element. If you add border, however, it is calculated from the bottom of the border.

    Also, when bottom and top margins are applied to elements that follow each other, they collapse, that is just the way the box model works.

    If you want to control the offset of an element inside another element, use padding.

提交回复
热议问题