【发布时间】:2020-02-13 22:03:54
【问题描述】:
.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 中添加一些内容,您会看到。现在你什么都没有,所以它没有显示并且 d2 离开了 margin-top。
-
这是默认行为。这里提出了类似的问题。 stackoverflow.com/questions/1762539/…