滚动文字渐变色需用background-clip: text+transparent color+渐变背景,配合animation或JS滚动监听实现;Firefox不支持该方案,可选SVG或分字着色替代。
滚动文字本身不能直接用 background: linear-gradient() 上色
因为 是废弃标签,且所有滚动效果实际都靠 animation + transform 或 scroll 实现;而文字内容(color)不支持渐变——CSS 里只有 background-clip: text 和 -webkit-text-fill-color: transparent 这套组合能“骗过”渲染引擎,把背景图/渐变“贴”到文字上。
常见错误是直接写:color: linear-gradient(...),这完全无效,浏览器会忽略。
用 background-clip: text 实现滚动文字渐变色
核心是三步:给文字设透明填充、铺渐变背景、裁剪背景到文字轮廓。必须加 -webkit- 前缀才能生效(目前仅 Safari/Chrome/Edge 支持,Firefox 仍不支持 text 值)。
-
color必须设为transparent,不能省略 -
background-image写linear-gradient(...),方向和色标按需调整 -
background-clip: text和-webkit-background-clip: text都要写全 - 滚动动画单独用
@keyframes+background-position动态偏移渐变位置,才能看到色彩流动感
h1 {
background: linear-gradient(90deg, #ff6b6b, #4ecdc4, #44b5b1);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
animation: gradientShift 6s ease-in-out infinite;
}
@keyframes gradientShift {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
滚动容器用 scroll 方案时渐变色怎么加
如果不用 CSS 动画,而是靠真实滚动(如长文案在固定高容器内 overflow-y: scroll),那渐变色就得作用在滚动内容本身,而不是靠位移动画。这时要注意: