神秘的底部填充/边距问题

理查德·罗杰斯

我有一个主要的.features类的div,在这个div中,我有两个框,每个框的高度设置为160px,宽度不同。如下面的屏幕截图所示,两个框的末端与主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;
}
詹姆斯·唐纳利

这实际上与padding无关margin如果查看计算的样式示例,我们将看到height元素本身的是164px

例

发生这种情况是因为您的内部元素设置为显示为inline-block这表示它们受的影响font-size,最终font-size导致父元素的高度大于内部元素的高度。

有两个修复程序:

  1. 在您的元素指定一个font-sizeof ,然后在内部元素中将其重置(通过为它们提供of ,或者使用您的默认大小)。0.featuresfont-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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章