【发布时间】:2009-11-06 04:07:42
【问题描述】:
在子元素 (h2) 上使用边距时,为什么它给父元素 (#box) 提供边距?如果我将边距更改为填充,它会按预期工作。 有什么改变还是我错过了什么?这是我的示例代码
<html>
<head>
<style>
#box{
border-radius:10px;
-moz-border-radius:10px;
-webkit-border-radius:10px;
height:200px;
width:500px;
background:red;
box-shadow: 15px 15px 12px grey;
-moz-box-shadow: 15px 15px 12px grey;
-webkit-box-shadow: 15px 15px 12px grey;
text-align:center;
margin-top:0;
}
#box h2{
color:#fff;
text-shadow: 2px 2px 2px #000;
margin-top:75px;/*making this padding gives the effect I thought I could achieve with margin*/
height:50px;
width:200px;
}
</style>
</head>
<body>
<div id="box">
<h2>Fun with CSS3</h2>
</div>
</body>
</html>
如果任何人或每个人都可以分享他们的保证金怪癖经验。太棒了
【问题讨论】:
-
您是在使用文档类型还是只是在示例中省略了它?
-
省略。对于这个例子来说,拥有一个并没有什么不同。
-
对于那些不知道这一点或关注问题的人。向#box 添加 1px 的 padding-top 可以让 h2 上的 margin-top 与 padding 一样工作。