简体   繁体   English

如何使用下面的内容构建响应式分层图像?

[英]How to build a responsive layered image with content below?

I've tested creating a layered image using a png with transparency over a jpg and using absolute positioning for the top image, however that upsets the positioning of content that should appear below the image. 我已经测试过使用png创建一个分层图像,透明度超过jpg,并使用绝对定位顶部图像,但这会扰乱应该出现在图像下方的内容的位置。 So then I tried without absolute positioning as described by Overlay Divs Without Absolute Position . 所以我尝试了没有绝对定位,如Overlay Divs Without Absolute Position所描述的那样。 This works for wider screens where the image width is 100%, but on smaller screens when the image width is scaled down the top image rides up because of the fixed negative top margin. 这适用于图像宽度为100%的较宽屏幕,但是当图像宽度按比例缩小时,在较小的屏幕上,由于固定的负上边距,顶部图像会上升。

Is there anyway to achieve a responsive layered image with content positioned correctly below without having to set the negative top margin using media queries? 无论如何要实现响应分层图像,内容定位正确,而不必使用媒体查询设置负上边距? I've created a test at 我已经创建了一个测试

https://jsfiddle.net/6v0Ls54o/3/ 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> 

If I understand it correct, you want to overlap 2 images on top of each other? 如果我理解它是正确的,你想要将两个图像重叠在一起吗?

I would use position: relative on #layeredImage and position: absolute on both img like this: 我会在#layeredImage上使用position: relative ,在这两个img上使用position: relative position: absolute

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

Using this code, both images will overlap withi their parent div. 使用此代码,两个图像将与其父div重叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM