登录

javascript - 子元素设置margin-top为什么会作用在父元素上?

如图,为什么我给p标签元素设置的margin-top会作用在p上面?

<p class="test">
    <p class="para">this is a para</p>
</p>
* {
    padding: 0;
    margin: 0;
}
.test {
    position: relative;
    height: 400px;
    width: 1000px;
    margin: 0 auto;
    margin-top: 0;
    margin-bottom: 0;
    background-color: #DCF1B8;
}
.para {
    width: 20%;
    margin-left: auto;
    margin-right: auto;
    background-color: #734E4E;
    height: 50%;
    margin-top: 100px;
    margin-bottom: 100px;
}
# JavaScript
黄舟黄舟2146 天前483 次浏览

全部回复(2) 我要回复

  • 怪我咯

    怪我咯2017-04-11 11:33:55

    CSS外边距合并

    http://www.w3school.com.cn/css/css_margin_collapsing.asp

    回复
    0
  • 高洛峰

    高洛峰2017-04-11 11:33:55

    因此推荐尽量优先使用padding来控制间距

    回复
    0
  • 取消回复发送