You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用CSS实现Spotify同款背景图片淡出效果?

实现Spotify风格的产品图背景淡出效果

这种效果可以通过HTML+CSS快速实现,核心思路是用两层元素:一层作为模糊渐变的背景,一层展示清晰的产品图。

实现步骤:

  • 准备一个容器,同时包含背景图和产品图两个子元素
  • 背景图设置为全屏覆盖,添加模糊滤镜和渐变遮罩,模拟淡出效果
  • 产品图居中放置在容器上层

代码示例:

HTML结构

<div class="product-container">
  <div class="product-bg"></div>
  <img class="product-img" src="你的产品图片路径" alt="产品图">
</div>

CSS样式

.product-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.product-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url("你的产品图片路径");
  background-size: cover;
  background-position: center;
  /* 添加模糊效果 */
  filter: blur(20px);
  /* 添加渐变遮罩,实现淡出 */
  mask: linear-gradient(to bottom, rgba(0,0,0,1) 60%, rgba(0,0,0,0));
  /* 兼容webkit内核浏览器 */
  -webkit-mask: linear-gradient(to bottom, rgba(0,0,0,1) 60%, rgba(0,0,0,0));
  /* 让背景图比容器大一圈,避免模糊后边缘露出空白 */
  transform: scale(1.1);
}

.product-img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 300px; /* 根据需求调整尺寸 */
  height: auto;
  border-radius: 8px; /* 可选,模拟Spotify的圆角 */
  box-shadow: 0 0 20px rgba(0,0,0,0.3); /* 可选,添加阴影增强层次感 */
}

注意事项:

  • 背景图和产品图使用同一张图片,保证风格统一
  • 模糊值(blur())和渐变的百分比可以根据实际需求调整,达到最接近的效果
  • 如果需要更精细的淡出效果,可以调整渐变的颜色节点,比如增加中间过渡色

内容的提问来源于stack exchange,提问作者Travis Mendes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 01:20:46