首页 > 解决方案 > 设置可滚动的背景图片

问题描述

我目前有这个小提琴:

https

: //jsfiddle.net/0g3u8452/1/ 我有一个 1920x4000 的图像,我希望它像

background-size: cover

但由于图像的高度很重要,我希望能够向下滚动以继续看到它

我拍了一张我想要得到的结果的照片:

在此处输入图像描述

预先感谢您的任何帮助 :)

标签: htmlcss

解决方案


尝试使用<img>withposition: absolute作为背景?

.container {
  position: relative;
}
.container img.background-img {
  position: absolute;
  width: 100%;
  top: 0;
  left: 0;
  z-index: -1;
}
body {
  padding: 0;
  margin: 0;
}
<div class="container">
  <img src="http://via.placeholder.com/1920x4000" class="background-img">
  <div class="other">Other elements</div>
</div>


推荐阅读