當在子元素(h2)上使用margin時,爲什麼會給父級(#box)留下餘量呢?如果我將邊距更改爲填充,則按預期工作。 做了些什麼改變或我錯過了什麼?這裏是我的示例代碼CSS邊緣怪異 - 請幫我理解
<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>
還是否有人或每個人都可以分享他們的保證金怪癖經驗。 THx
您使用DOCTYPE還是你只是從你的例子忽略它?省略了 – 2009-11-05 20:06:50
。對這個例子有一個沒有什麼不同。 – chris 2009-11-05 22:42:11
對於那些不知道或不遵守問題的人。將1px的填充頂部添加到#box可讓h2上的margin-top與填充工作相同。 – chris 2009-11-05 22:43:04