【问题标题】:positioning scalable image on top of other image将可缩放图像定位在其他图像之上
【发布时间】:2014-12-23 06:32:13
【问题描述】:

我正在处理一个项目,我想将一张图片放在另一张图片之上。我已经能够弄清楚如何做到这一点,但是一旦我尝试将图像居中并使其可扩展,它将不再以我想要的方式显示。

我希望黄色图像(横幅)显示在主图片(top-pic)的顶部。我希望横幅相对于顶部图片居中,距顶部图片底部有 60 像素的边距。有谁知道如何定位它,同时允许两张图片都可以缩放?

My JSFiddle

Updated JSFiddle but still not working

我的 CSS:

.top-pic img {
  max-width: 100%;
  height: auto;
  min-height: 35%;
  width: auto\9; /* ie8 */
}

.banner-wrap {
  width: 100%;  
  position: absolute;
}

.banner {
  width: 796px;
  margin: 0px auto;
}

.banner img {
  position: absolute;
  bottom: 59px;
  width: auto;
  height: auto;
}

我的 HTML:

<section>
  <div align="center" style="background-color:#EAC40D;">
    <div class="top-pic">
      <div>
        <img src="http://www.affordablehomecare.org/Templates/assets/images/slider/1.jpg">
      </div>
    </div>
  </div>
  <div class="banner-wrap">
    <div class="banner">
      <img class="banner" src="assets/images/banner_title_home.png">
    </div>
  </div>
</section>

【问题讨论】:

    标签: html css image css-position


    【解决方案1】:

    在您的first fiddle 中,您正在基于banner 类设置img 的样式:

    .banner img {
      position: absolute;
      bottom: 59px;
      width: auto;
      height: auto;
    }
    

    但是,您没有该类的元素,因此样式被忽略。

    改成这样:

    .banner-wrap img {
      ...
    }
    

    绝对定位元素相对于任何定位的父元素,如果没有定位父元素,则为body 元素。在您的第一个小提琴中,图像的父级是 not 定位的。你可以这样补救:

    .banner-wrap {
      position: relative;
    }
    

    最后,将widthheight 设置为自动会阻止您的图像可扩展。将height 保留为自动,但将width 更改为百分比:

    .banner img {
      position: absolute;
      bottom: 59px;
      width: 100%;
      height: auto;
    }
    

    您的图像现在应该都可以扩展了。

    Fiddle

    【讨论】:

    • 你是学者还是神!太感谢了。你不知道我在这个问题上工作了多久。另外,感谢教育。总有东西要学!
    【解决方案2】:

    删除包装 div,只使用 img 元素。

    <section>
        <div align="center" style="background-color:#EAC40D;">
            <div class="top-pic">
                <div><img src="http://www.affordablehomecare.org/Templates/assets/images/slider/1.jpg"></div>                
            </div>
            <img src="http://www.affordablehomecare.org/Templates/assets/images/banner_title_home.png">
        </div>         
    </section>
    

    并使用这个 css:

    .top-pic img {
        max-width: 100%;
        height: auto;
        min-height:35%;
        width: auto\9; /* ie8 */}
     img{
        position:absolute;
        bottom:59px;
        width: auto;
        height: auto;}
    

    我已经修改了您的 JSFiddle 以使其正常工作。在这里:

    http://jsfiddle.net/7wry4w0p/5/
    

    【讨论】:

    • 感谢 Nelson,但它在整个项目中对我不起作用。只要我在该位置下方添加文本,该位置就会再次关闭。此外,横幅图像不会缩放。我更新了我的 JSFiddle。有什么想法吗?
    • 只是位置问题。看这里:jsfiddle.net/vfvwxvt3我只是在一些地方添加了左、下和宽度。
    • 似乎仍然卡在一个地方。当您更改窗口大小时,横幅不会保持在顶部图片的中心
    • 那是因为它使用绝对定位。我虽然这就是你想要的。换成相对的,看看情况如何。
    • 还是不行。这就是为什么我把它放在这个网站上。整天拉着我的头发试图让它在没有运气的情况下工作......
    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 2013-06-28
    • 2016-11-16
    • 2012-09-24
    • 2016-02-19
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多