在css正文中调整背景图像的大小,但避免重复



我正在尝试添加此图像https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Coffee_and_Friends_(Unsplash).jpg/2560px-Coffee_and_Friends_(Unsplash).jpg作为body选择器的背景图像。

这是我的代码

body {
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Coffee_and_Friends_%28Unsplash%29.jpg/2560px-Coffee_and_Friends_%28Unsplash%29.jpg");
background-size: cover;
background-repeat: no-repeat;
}

它工作得很好,但在某些尺寸的图像开始重复,所以我添加平铺方式:不再重演;在代码。

现在在重复的图像有空白的地方,在窗口,如何用单个图像填充这个空白?

添加background-positionbackground-attachment属性:

  • background-position将设置背景图像的起始位置
  • background-attachment(设置为fixed)将防止你的背景图片随着页面内容滚动

body {
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Coffee_and_Friends_%28Unsplash%29.jpg/2560px-Coffee_and_Friends_%28Unsplash%29.jpg");
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
p {
font-size: 36px;
font-family: sans-serif;
color: #fff;
text-align: center;
}
<body>
<p>
Some text
</p>
</body>

最新更新