【问题标题】:How to build a responsive layered image with content below?如何构建具有以下内容的响应式分层图像?
【发布时间】:2019-09-25 02:30:53
【问题描述】:

我已经测试过使用在 jpg 上具有透明度的 png 并为顶部图像使用绝对定位来创建分层图像,但这会扰乱应显示在图像下方的内容的定位。因此,我尝试不使用Overlay Divs Without Absolute Position 描述的绝对定位。这适用于图像宽度为 100% 的较宽屏幕,但在较小的屏幕上,当图像宽度按比例缩小时,由于固定的负上边距,顶部图像会向上移动。

有没有办法实现响应式分层图像,内容正确定位在下方,而无需使用媒体查询设置负上边距?我在

创建了一个测试

https://jsfiddle.net/6v0Ls54o/3/

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#productimage {
  background: #fff;
  max-width:486px;
  text-align:left;
  padding:0 20px 0 20px;
  margin:0;
  height:auto;
}

#layeredImage{
  margin:20px 0 40px 0;
  width:100%;
  height:auto;
  float:left
}

#layeredImage img{
  width:100%;
  height:auto;
}

#topImage{
  margin-top:-300px;
  float:left;
  position:relative;
  overflow:visible;
  z-index:1
}

#belowImage{
  float:left
}

.clear{
  clear:both;
  font-size:0;
  line-height:0;
  height:0;
  overflow:hidden;
  margin:0
}
<div id="productimage">
  <div class="titleWrapper">
    <h1>How to build a responsive layered image?</h1>
  </div>
  <div id="layeredImage">
    <img src="https://picsum.photos/486/300" alt="" width="486" height="300" id="bottomImage" />
    <img src="https://picsum.photos/486/300" alt="Top Image" title="Top Image" width="486" height="300" id="topImage" />
    <div class="clear"></div>
  </div>
  <div id="belowImage"><h2>Some info to appear below the image:</h2>
  <p>
  Is there a way to build a responsive layered image where content can appear below the layered image?
  </p></div>
  <div class="clear"></div>
</div>

【问题讨论】:

  • 如果您只定位 一个 图像绝对,另一个仍然会跨越容器...
  • @04FS 这似乎是答案,谢谢。比我想象的要简单得多! :)
  • @04FS 我已经更新了 jsFiddle 以证明它可以在 jsfiddle.net/6v0Ls54o/4 工作,你会发表你的评论作为答案吗?

标签: css responsive layered


【解决方案1】:

如果我理解正确,您想将 2 张图像重叠在一起吗?

我会在#layeredImage 上使用position: relative,在两个img 上使用position: absolute,如下所示:

#layeredImage {
   position: relative;
 }
img {
   position: absolute;
   left: 0
   right: 0
 }

使用此代码,两个图像将与其父 div 重叠。

【讨论】:

  • 两个图像的绝对位置会导致应该在图像下方的内容不显示。我相信这是由于“绝对”导致#layeredImage div 的高度丢失。这可以在jsfiddle.net/6v0Ls54o/5 看到答案是只设置#topImage 的绝对位置,如04FS 在他们上面的评论中所建议的那样,如jsfiddle.net/6v0Ls54o/4
猜你喜欢
  • 1970-01-01
  • 2017-02-03
  • 2014-10-30
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-31
相关资源
最近更新 更多