【问题标题】:Image resizes to max-height but page overflows图像大小调整为最大高度但页面溢出
【发布时间】:2016-08-03 08:22:56
【问题描述】:

我有一个img,它使用max-height: 100% 调整到页脚高度(10vh)。在开发工具中,每个元素的大小似乎还可以,但不知何故页面溢出了,我无法弄清楚额外的 px 高度是从哪里来的。 这是我的代码:

* {
  margin: 0 !important;
  padding: 0 !important;
}
body {
  background-color: yellow;
}
.header {
  height: 10vh;
  background-color: red;
}
.content {
  height: 80vh;
  background-color: green;
}
.footer {
  height: 10vh;
  background-color: blue;
}
img {
  max-height: 100%;
  max-width: 100%;
}
<div class="header">

</div>
<div class="content">

</div>
<div class="footer">
  <img src="https://pixabay.com/static/uploads/photo/2016/01/19/18/00/city-1150026_960_720.jpg" alt="" />
</div>

为什么会发生这种情况,我该如何避免?

更新:我不知道&lt;img&gt; 的默认display: inline 导致了这种情况。现在我知道找到我的问题的其他答案要容易得多(我只是不知道要搜索什么)。对于那些可能正在搜索此问题并在此处找到我的问题的人,这是一个完整的答案:https://stackoverflow.com/a/31445364/6453726

解决方案:将vertical-align: top 添加到&lt;img&gt;

【问题讨论】:

  • 因为你的图片是内联元素,read
  • 谢谢。如果我只是将 display: block 添加到 img,我不会明白发生了什么。

标签: html css image height overflow


【解决方案1】:

display:block; 添加到您的img 选择器

img {
    max-height: 100%;
    max-width: 100%;
    display: block;
}
猜你喜欢
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 2018-09-10
  • 2013-04-03
  • 2015-11-22
  • 1970-01-01
  • 2016-03-30
  • 2017-12-08
相关资源
最近更新 更多