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:

  1. Apply an oversized linear-gradient() to the element using background-size: 300% auto. By repeating the starting color at the end of the gradient string, we ensure the loop is seamless.
  2. Make the text color transparent (color: transparent) and use background-clip: text (and its -webkit- prefix) to restrict the background so it only renders where the text characters are drawn. Note that background-clip handles text differently across browsers, hence the prefix.
  3. Set up an @keyframes block to endlessly animate the background-position horizontally across the oversized canvas.

Here is the code to create the Text Gradient Shift animation.

View Output

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; 
    }
}