边际增加父亲

时间:2017-05-02 09:04:05

标签: html css html5 css3

你好,为什么孩子的边缘底部没有增加父母的身高:

<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;
}

enter image description here

但是如果为父级添加边框,则父级的高度会增加:

.my-container {
    border:1px solid;
}

enter image description here

所以我想了解为什么会出现这种差异?

2 个答案:

答案 0 :(得分:2)

您需要将overflow: auto;添加到父div。

&#13;
&#13;
.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;
&#13;
&#13;

答案 1 :(得分:0)

对我的容器使用display: inline-blockwidth: 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%;
}