Margin goes outside div when border is removed

前端 未结 4 1902
后悔当初
后悔当初 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:54

    body{/* just a reset to simplify example */
        padding:0;
        margin:0
      }
      .inner {
        margin-top:40px;
        height:40px;
        width:40px;
        background-color:blue;
      }
      #outer{
        background-color:green;
        height:60px;
        width:60px;
      }
    test

    Above code is a more general case of the problem you mentioned. All .inner are having margin-top=40px in CSS. But in reality, since there is no border involved, all the margins just collapse down to one final margin of 40px which "bubble up" outside of root parent.

提交回复
热议问题