可以使用相对定位(position: relative;
)来使元素相对于其父元素进行定位。然后使用负的左偏移量(left
)和上偏移量(top
)来调整元素的位置,以使其与移动的背景图保持相对位置。
代码示例:
HTML:
CSS:
.container {
height: 500px;
background: url('background.png') no-repeat;
background-size: cover;
position: relative; /* 父元素使用相对定位 */
}
.element {
width: 50px;
height: 50px;
background-color: red;
position: relative; /* 子元素使用相对定位 */
left: -100px; /* 使用负的左偏移量来调整元素的位置 */
top: 150px; /* 使用负的上偏移量来调整元素的位置 */
}
下一篇:背景图由超过页脚的SVG制成