【问题标题】:CSS responsive overlayed imagesCSS 响应式叠加图像
【发布时间】:2018-12-18 22:24:17
【问题描述】:

我有两张图片需要相互重叠。图像还必须具有响应性,因此具有百分比宽度和高度。

<div class="container">
    <img src="res/bigger.png/>
    <img src="res/smaller.png class="icon"/>
</div>

.container {
  width:100%;
  height:100%;
  background-color:blue;
  postion:relative;
}
.container img {
   max-width:100%;
   max-heihgt: 100%;
   height: auto;
   width: auto;
}
.icon {
   position: relative;
   top: -70%;
   left: 20%;
   z-index: 50;
   width: 10% !important;
   height: auto !important;
}

由于两个图像在调整大小时的比例不同,因此位于较大图像顶部的较小图像将失去其相对于较大图像的位置。重新调整页面大小时,如何保持较小图像相对于较大图像的位置?
这个问题的一个例子可以在这里找到http://jsfiddle.net/5YQFV/

【问题讨论】:

    标签: html image css responsive-design


    【解决方案1】:

    不如使用这样的容器替换最大的图像,而不是使用两个图像

    <div class="the-gang">
      <img src="res/smaller.png" />
    </div>
    

    然后像这样将容器的位置设置为position:relative,但将较小图像的位置设置为position:absolute

    .the-gang{
      position:relative;
      width:100px;
      height:75px;
    }
    
    .the-gang img{
      position:absolute;
      top:10px;
      left:10px;
    }
    

    这样最小的图像将始终保持在您放置的位置,因为绝对位置将相对于它的容器。 可能的缺点是您必须设置父容器的最小高度和宽度,但优点是它将完全在 CSS 中完成,无需使用 javascript。

    【讨论】:

    • 我已经按照你的解释更新了小提琴:jsfiddle.net/5YQFV/2 这个例子的问题是 div 没有调整到图像的大小,因此较小的图像仍然会作为容器移动div 已调整大小
    • 你的意思是容器没有调整到背景图片的大小吗?如果是这样,那么您必须使用 javascript 来获取背景的大小,因为 css 不知道它的大小并且无法调整容器的大小。
    猜你喜欢
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 2014-10-20
    相关资源
    最近更新 更多