html - div 中的图像在图像下方有额外的空间

标签 html css image

为什么在following code div 的高度大于 img 的高度?图像下方有一个间隙,但它似乎不是填充/边距。

图片下方的空隙或额外空间是什么?

#wrapper {
  border: 1px solid red;
  width:200px;
}
img {
  width:200px;
}
<div id="wrapper">
  <img src="http://i.imgur.com/RECDV24.jpg" />
</div>

Image with a gap or white space under it

最佳答案

默认情况下,图像是内嵌渲染的,就像字母一样,所以它与 a、b、c 和 d 位于同一行。

descenders 行下方有空间你可以在 g、j、p 和 q 等字母上找到。

Demonstration of descenders

您可以:

  • 调整vertical-align将其定位到其他位置(例如 中间)
  • 更改显示,使其不是内联的。

div {
  border: solid black 1px;
  margin-bottom: 10px;
}

#align-middle img {
  vertical-align: middle;
}

#align-base img {
  vertical-align: bottom;
}

#display img {
  display: block;
}
<div id="default">
<h1>Default</h1>
  The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt="">
</div>

<div id="align-middle">
<h1>vertical-align: middle</h1>
  The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt=""> </div>
  
  <div id="align-base">
<h1>vertical-align: bottom</h1>
  The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt=""> </div>

<div id="display">
<h1>display: block</h1>
  The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt="">
</div>


包含的图像是公共(public)领域和 sourced来自维基共享资源

关于html - div 中的图像在图像下方有额外的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52395324/

相关文章:

html - 降低列表样式以适合文本 css

html - 子位置绝对父相对不起作用

html - 我的图像映射覆盖了我的整个网站、文字、表格等,如何解决?

javascript - 如何在 JavaScript 中 move 图像?

ios - 图标在 iPhone 主屏幕上看起来很奇怪

html - 背景图像适合选项?

javascript - 点击前显示内容的 JQuery Accordion ?

javascript - 将复杂属性数据加载到数组数组中时遇到问题

jQuery 手机 : difference between data-role and role/class

html - 在行/列中垂直堆叠元素而不是水平