<分区>
标签 javascript css
<分区>
Possible Duplicate:
Margin on child element moves parent element
我有一个包含子 div 的父 div。 Parent
是正文中的第一个元素,没有特定的 CSS 样式。当我设置
.child {
margin-top: 10px;
}
最终结果是 child
的顶部仍然与 parent
对齐。不是 child
向下移动 10px,而是我的 parent 向下移动 10px。
我发现向 parent
添加填充可以工作,但我读到这不是解决问题的方法。为什么?如果我想要 margin-right
我可以在 parent
上做 padding-right
,同样如果我想要 margin-top
我也可以执行 padding-top
并获得正确的结果。
在margin on child element moves parent element有人说:
What if you want a top-margin...? Not really a solution. This isn't really a solution if a top margin is required.
他想表达什么?请解释。
最佳答案
来自 CSS Margin Collapsing在 Mozilla 开发者网络上:
Parent and first/last child
If there is no border, padding, inline content, or clearance to separate the margin-top of a block with the margin-top of its first child block [...] then those margins collapse. The collapsed margin ends up outside the parent.
关于javascript - 子元素的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14110488/