CSS Animation Example: Text Gradient Shift
The "Text Gradient Shift" provides a highly modern, premium feel to large headings and hero sections by continuously panning a colorful gradient inside the letters.
Gradient
There are three core steps to achieving this effect:
- Apply an oversized
linear-gradient()to the element usingbackground-size: 300% auto. By repeating the starting color at the end of the gradient string, we ensure the loop is seamless. - Make the text color transparent (
color: transparent) and usebackground-clip: text(and its-webkit-prefix) to restrict the background so it only renders where the text characters are drawn. Note thatbackground-cliphandles text differently across browsers, hence the prefix. - Set up an
@keyframesblock to endlessly animate thebackground-positionhorizontally across the oversized canvas.
Here is the code to create the Text Gradient Shift animation.
Accessibility Considerations
Rapidly shifting colors, especially high-contrast neon ones, can be a migraine trigger or highly distracting for users with ADHD. prefers-reduced-motion is a CSS media feature used to detect if a user has enabled a setting on their device to minimize the amount of non-essential animation or motion.
We handle this gracefully by removing the animation altogether. The text will still look gorgeous with a static gradient applied to it for users who have enabled prefers-reduced-motion on their device.
Here's what we used in our example:
@media (prefers-reduced-motion: reduce) {
.gradient-text {
/* Stops the continuous color shifting */
animation: none;
/* Locks the gradient to its starting position */
background-position: 0% center;
}
}