【问题标题】:Mysterious bottom padding/margin issue神秘的底部填充/边距问题
【发布时间】:2015-11-03 17:29:04
【问题描述】:

我有一个 .features 类的主 div,在这个 div 内我有两个框,每个框的高度设置为 160 像素,宽度不同。在两个盒子的末端和主 div 之间有一个神秘的填充,如下面的屏幕截图所示:

内边距约为 5 像素 - 如果可能,我想删除此内边距。我尝试添加边距:0;和填充:0;到主 div 以及两个内框,但它不起作用。

这是页面这一部分的 html:

<div class="features">
    <div class="list-items"></div>
    <div class="screenshot-box"></div>
</div>

CSS:

 .features {
    width: 980px;
    margin: auto;
    margin-top: 25px;
    background-color: lightblue;
}

.list-items {
    width: 280px;
    height: 160px;
    display: inline-block;
    background-color: red;
}

.screenshot-box {
    width: 583px;
    height: 160px;
    float: right;
    padding-bottom: 0;
    display: inline-block;
    background-color: red;
}

【问题讨论】:

  • 你能把你的代码放在小提琴上吗?

标签: html css height


【解决方案1】:

这实际上与paddingmargin 无关。如果我们查看计算样式示例,我们会看到元素本身的height164px

这是因为您的内部元素设置为显示为inline-block。这意味着它们受到font-size 的影响,最终font-size 导致父元素的高度大于内部元素的高度。

有两个修复:

  1. 在您的.features 元素上指定font-size0,然后在内部元素中重置它(通过给它们一个font-size16,或您的默认大小)。李>

.features {
  width: 980px;
  margin: auto;
  margin-top: 25px;
  background-color: lightblue;
  font-size: 0;
}

.list-items {
  width: 280px;
  height: 160px;
  display: inline-block;
  background-color: red;
  font-size: 16px;
}

.screenshot-box {
  width: 583px;
  height: 160px;
  float: right;
  padding-bottom: 0;
  display: inline-block;
  background-color: red;
  font-size: 16px;
}
<div class="features">
    <div class="list-items"></div>
    <div class="screenshot-box"></div>
</div>
  1. 给你的.features 元素一个height160px 本身来匹配它的子元素。有了这个,浏览器就不必自己计算高度了。

.features {
  width: 980px;
  margin: auto;
  margin-top: 25px;
  background-color: lightblue;
  height: 160px;
}

.list-items {
  width: 280px;
  height: 160px;
  display: inline-block;
  background-color: red;
}

.screenshot-box {
  width: 583px;
  height: 160px;
  float: right;
  padding-bottom: 0;
  display: inline-block;
  background-color: red;
}
<div class="features">
    <div class="list-items"></div>
    <div class="screenshot-box"></div>
</div>

【讨论】:

  • 谢谢詹姆斯,我采用了字体大小方法,因为我希望父元素在添加新内容时自行扩展它。现在,我明白为什么那个填充物在那里了。我每天都在学习更多。再次感谢!
【解决方案2】:

只需将font-size 设为.features 的0,它将占用全宽。 Here 是你的小提琴。

.features {
  width: 980px;
  margin: auto;
  margin-top: 25px;
  background-color: lightblue;
  font-size: 0;
  /*Just make font size as 0*/
}
.list-items {
  width: 280px;
  height: 160px;
  display: inline-block;
  background-color: red;
}
.screenshot-box {
  width: 583px;
  height: 160px;
  float: right;
  padding-bottom: 0;
  display: inline-block;
  background-color: red;
}
<div class="features">
  <div class="list-items"></div>
  <div class="screenshot-box"></div>
</div>

【讨论】:

    【解决方案3】:

    您也可以在两个子元素上放弃display: inline-block,并在.list-items 上设置float: left,在.features (code example) 上设置display: table。增加了没有硬编码父 div 高度的好处,父 div 将扩展以适应子内容。

    @james donnelly 已经给你一个准确简洁的原因解释了。

    【讨论】:

    • 我不愿意采用这种方法,因为历史上不同的浏览器对表格显示元素的处理方式不同,浮动元素(table-rowtable-cell 都不是)并不是真正打算放置的在他们之中。在两个元素上使用float 的更好方法是事后清除浮动。
    猜你喜欢
    • 1970-01-01
    • 2012-02-16
    • 2013-10-15
    • 2012-07-27
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    相关资源
    最近更新 更多