<分区>
.d1 {
background-color: lightblue;
}
.d2 {
background-color: yellow;
margin-top: 20px;
}
<div class="d1">
<div class="d2">Test</div>
</div>
在这个简单的例子中,d1
是d2
的父元素。但是 d2
导致它自己和 d1
都有 margin-top
的 20px
。为什么会这样?
<分区>
.d1 {
background-color: lightblue;
}
.d2 {
background-color: yellow;
margin-top: 20px;
}
<div class="d1">
<div class="d2">Test</div>
</div>
在这个简单的例子中,d1
是d2
的父元素。但是 d2
导致它自己和 d1
都有 margin-top
的 20px
。为什么会这样?
最佳答案
它的简单逻辑是,当您使用父 div 和子 div 时,父 div 将被子 div 样式覆盖,因为父 div 将检查所有内部样式并为您提供输出。
示例:
如果您在 d1 和 d2 中都使用 margin-top: 20px;
,它将为您提供顶部 40px 的输出。
关于html - margin top 导致父 div 下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58424907/