你好,为什么孩子的边缘底部没有增加父母的身高:
<div class="my-container">
<div class="margin">
Hello World
</div>
<div class="margin">
Hello World
</div>
.my-container {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
width: 300px;
background-color: #3873fe;
margin: auto;
}
.my-container .margin {
width: 100px;
margin: 0 auto 50px;
background-color: #00ff82;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
但是如果为父级添加边框,则父级的高度会增加:
.my-container {
border:1px solid;
}
所以我想了解为什么会出现这种差异?
答案 0 :(得分:2)
您需要将overflow: auto;
添加到父div。
.my-container {
overflow: auto;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
width: 300px;
background-color: #3873fe;
margin: auto;
}
.my-container .margin {
width: 100px;
margin: 0 auto 50px;
background-color: #00ff82;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
&#13;
<div class="my-container">
<div class="margin">
Hello World
</div>
<div class="margin">
Hello World
</div>
</div>
&#13;
答案 1 :(得分:0)
对我的容器使用display: inline-block
和width: 100%
。
Fiddle
<强> CSS 强>
.my-container {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
width: 300px;
background-color: #3873fe;
margin: auto;
}
.my-container .margin {
width: 100px;
margin: 0 auto 50px;
background-color: #00ff82;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
display: inline-block;
width: 100%;
}