Margin on child element moves parent element

前端 未结 14 2172
广开言路
广开言路 2020-11-21 23:38

I have a div (parent) that contains another div (child). Parent is the first element in body with no

14条回答
  •  名媛妹妹
    2020-11-22 00:35

    interestingly my favorite solution to this problem isn't yet mentioned here: using floats.

    html:

    css:

    .parent{width:100px; height:100px;}
    .child{float:left; margin-top:20px; width:50px; height:50px;}
    

    see it here: http://codepen.io/anon/pen/Iphol

    note that in case you need dynamic height on the parent, it also has to float, so simply replace height:100px; by float:left;

提交回复
热议问题