html - 尽管最大高度设置为 100%,但图像变得比父级大

标签 html css

我正在尝试使用 <img>标记在另一个 div 上显示照片,作为某种覆盖。但是,图像不会缩放到其父 div(页面主体)内。相反,它溢出了 body 。当我设置 overflow: hidden;到正文,你不能滚动页面。我希望图像是全高的并适合 body (不扩大 body )。

这是页面的基本结构:

<html>
   <body>
      <div class="imgContainer">
         <img class="actualImage" />
      </div>
      <div class="restOfBody">
      </div>
   </body>
</html>

还有CSS:

body {
  background-image: url(*some background photo*)
  background-repeat: no-repeat;
  background-size: cover;
  width: 100%;
  padding: 0;
  margin: 0;
  max-height: 100%;
}

.imgContainer {
  z-index: 2;
  position: absolute;
  top: 0;
  left: 0;
  max-height: inherit;
}

.actualImage {
  max-height: 100%;
}

这基本上就是现在发生的事情: The image that is drawn over the text right now, pushes the page down so far, that it actually exceeds the body of the html.

最佳答案

在您使用 meta 作为高度之前,以 % 表示的高度将不起作用, ok请在overflow:none处使用overflow:hidden

关于html - 尽管最大高度设置为 100%,但图像变得比父级大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38934133/

相关文章:

html - 在输入字段中突出显示文本/标记 - 松弛搜索框样式

html - CSS 动画不会在 IE11 上的 iframe 中播放

html - 如何相对于内部绝对高度调整相对div高度?

javascript - 在我的案例中如何将显示类型更改为内联元素?

javascript - VueJs 观察对象的深层变化

css - Firefox 3.6 破坏了我的页面样式!

html - 从具有每日存档的静态 HTML/CSS 单页站点创建可搜索存档

javascript - 如何在加载背景时加载动画

html - 使用 Bootstrap 网格自动调整 iframe 的大小

带有填充的 CSS 背景叠加颜色问题