首页 > 解决方案 > 图像下方的空白

问题描述

我在我的 css 项目中遇到了一些问题。我试图将图像放在 div 中,并以某种方式在图像下方放置了一个空白。

在此处输入图像描述

这是我的html代码:

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

#top {
  border: solid;
}

#page-top {
  border: solid;
  width: 1028px;
  background: yellow;
  margin: 0 auto;
}

#page-top img {
  width: 30%;
  border: solid;
  display: inline-block;
}
<div id="top">
  <div id="page-top">
    <img src="https://i.imgur.com/7nqdfTK.png">

  </div>
</div>

标签: htmlcss

解决方案


内联元素的默认垂直对齐方式是基线,它为下行文本元素保留空间。通过将垂直对齐属性设置为顶部或中间来解决此问题。

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

#top {
  border: solid;
}

#page-top {
  border: solid;
  width: 1028px;
  background: yellow;
  margin: 0 auto;
}

#page-top img {
  width: 30%;
  border: solid;
  display: inline-block;
  vertical-align: middle;
}
<div id="top">
  <div id="page-top">
    <img src="https://i.imgur.com/7nqdfTK.png">

  </div>
</div>


推荐阅读