首页 > 解决方案 > 相对于背景定位图像

问题描述

我希望图像的定位使其中心始终位于蓝色背景的底部。它应该如下所示: 在此处输入图像描述

我可以使用 top:37v; 但是调整窗口高度不会让它保持相对于背景的位置。

.background{
    background-image: url("https://i.imgur.com/5Y5F5fF.png");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 0 100%;
    height: 50vh;
}
header img{
    position: relative;
    height: 100px;
    /*!*top: 37vh;*! Don't want to use this as resizing will effect its position relative to background*/
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<header>
        <div class="container-fluid d-flex justify-content-center background">
            <img src="https://i.imgur.com/3dzn4KM.png" alt="phone">
        </div>

    </header>

标签: htmlcssbootstrap-4

解决方案


将这 2 行添加到样式中:

header img{
     position: relative;
     height: 100px;
     top:50%;
     margin-top:-50px;
 }

这应该会有所帮助。


推荐阅读