html - 使用 margin-top 时出现空白

标签 html css

我正在尝试使用 margin-top 将容器相对于其父容器向下推。但是,当尝试这样做时,父 div 和前面的 div 之间会出现白色间隙(请参见屏幕截图)。我想知道是否有人有解决方案enter image description here为此。

enter image description here

enter image description here

最佳答案

如果问题是背景没有被填充,则使用填充,而不是边距。这适用于父 div,而不是子 div:

http://jsfiddle.net/tmd4thpa/

.container {
  background: purple;
  padding:20px;
}
.div {
  height:500px;
  background: red;
}
<div class="container">
  <div class="div"></div>
</div>

关于html - 使用 margin-top 时出现空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26660098/

相关文章:

javascript - 通过 JavaScript 覆盖类

html - 如何使用密码保护托管在 Amazon Web Services (AWS) 上的网站

html - CSS 高度 100%

html - 输入类型 ="number"在 Firefox 中工作错误

javascript - 为什么它不改变字体大小?

c++ - Qt:更改 html 标签的颜色不起作用

javascript - img 响应式,固定样式

html - 在水平显示中添加菜单项

css - 绝对/固定位置 : preventing items scrolling

javascript - FileReader onload 仅在浏览器中第二次工作