首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尽管max-height设置为100%,但图像会变得比父图像大

尽管max-height设置为100%,但图像会变得比父图像大
EN

Stack Overflow用户
提问于 2016-08-13 23:16:41
回答 3查看 384关注 0票数 0

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

这基本上就是页面的结构:

代码语言:javascript
复制
<html>
   <body>
      <div class="imgContainer">
         <img class="actualImage" />
      </div>
      <div class="restOfBody">
      </div>
   </body>
</html>

和css:

代码语言:javascript
复制
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.

EN

回答 3

Stack Overflow用户

发布于 2016-08-13 23:25:13

在你使用meta作为高度之前,%中的高度将不起作用,ok,请使用overflow:隐藏在overflow:none的位置

票数 0
EN

Stack Overflow用户

发布于 2016-08-13 23:32:57

使您的图像作为背景图像。我觉得这样会更好。

票数 0
EN

Stack Overflow用户

发布于 2016-08-13 23:33:20

在您的身体中添加一个容器,如下所示:

代码语言:javascript
复制
<body>
    <div class="container">
        <div class="imgContainer">
            <img class="actualImage" src="banner.jpg">
        </div>
    </div>
</body>

并在css中执行此操作

代码语言:javascript
复制
.container {
    width: 200px;/*sample width*/
    height: 200px;/*sample height*/
    overflow: hidden;
}
.imgContainer {
  position: relative;
  top: 0;
  left: 0;
  max-height: inherit;
}

.container负责设置边界,并通过使用overflow: hidden防止.container中的内容重叠。而在.imgContainer的情况下,确保位置是相对于容器的,绝对将自己拉出流,这在您的情况下是不安全的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38934133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档