html - 边距增加父级的高度

标签 html css

您好,为什么子级的边距底部不增加父级的高度:

<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

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

最佳答案

您需要将 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;
}
<div class="my-container">
  <div class="margin">
    Hello World
  </div>
  <div class="margin">
    Hello World
  </div>
</div>

关于html - 边距增加父级的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43733994/

相关文章:

javascript - <img> 的 XSS 上的数据 URI 是否可被利用?

html - 如何确保 Canvas 与html中的视频大小相同?

Php 没有返回正确的 MIME 类型

html - 自动换行 : break-word does not work with a very long word combined with width: 100%

css - 在我们网站的 2 个页面上,Fire Fox 标题上方的空间。其他还行

javascript - 在下拉列表中单击复选框时,复选框保持选中状态

asp.net - 通过 css 本地化图像

javascript - SharePoint 2013 HTML5 Javascript 使用 CSOM/JavaScript 上传到文档库

html - 'min-height: 100vh' 有子元素在width 小的情况下堆叠是如何工作的?

jquery - 表格滚动条在 chrome 中被锁定