Margin on child element moves parent element

前端 未结 14 2210
广开言路
广开言路 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:17

    This is normal behaviour (among browser implementations at least). Margin does not affect the child's position in relation to its parent, unless the parent has padding, in which case most browsers will then add the child's margin to the parent's padding.

    To get the behaviour you want, you need:

    .child {
        margin-top: 0;
    }
    
    .parent {
        padding-top: 10px;
    }
    

提交回复
热议问题