Tom Dohnal

State-of-the-art frontend development

Back to postsWatch video
November 2, 2022 β€’ 8 min read

Animated gradient texts

Let's learn how to create animated gradient texts (the ones you might know from vercel.com). This is what we'll build! πŸ‘‡πŸ»

Animated.

Gradient.

Text.


We'll use plain HTML and CSS but feel free to use your favorite UI or styling library as you follow along!

Creating gradient text (w/o animation)

First, we'll create a text with a gradient background but no animation.

For starters, let's put some HTML markup to work with together and apply some boilerplate CSS styles:

<head>
  <style>
    body {
      background: #000;
      padding: 64px;
      margin: 0;
      font-family: sans-serif;

      /* make the rendered glyphs a bit smoother around the edges */
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
    }

    h1 {
      margin: 0;
      font-size: 160px;
      font-weight: 800;
      color: #fff;
      letter-spacing: -0.03em;
    }

    .heading-line {
      line-height: 1;
      display: block;
      position: relative;
      text-align: center;
    }
  </style>
</head>

<body>
  <h1>
    <span class="heading-line heading-line-first">Animated.</span>
    <span class="heading-line heading-line-second">Gradient.</span>
    <span class="heading-line heading-line-third">Text.</span>
  </h1>
</body>

If you'd like, you could use the fancy pants nth-child(...) selectors instead of heading-line-first etc. πŸ˜‡

To add the gradients, we'll set the color to transparent, the background-image to linear-gradient(...) with our favorite colors, and background-clip to text to only render the gradient within the boundaries of the text. πŸ‘‡πŸ»

.heading-line {
  /* ... */

  /* set the color of the text to transparent */
  color: transparent;

  /* we need to clip the gradient background so that
  so only the text bit will have the gradient background
  (otherwise the entire rectangle the text is in would have it) */
  -webkit-background-clip: text;
  background-clip: text;
}

.heading-line-first {
  background-image: linear-gradient(to left, #ed8936, #ed64a6);
}

.heading-line-second {
  background-image: linear-gradient(to left, #38b2ac, #0bc5ea);
}

.heading-line-third {
  background-image: linear-gradient(to left, #ecc94b, #48bb78);
}

This is what you should end up with πŸ‘‡πŸ»

Animated.

Gradient.

Text.

Animating the gradient

In this section, we're only going to animate the gradient background. To add some more spice to the animation, we're also going to animate the white background of each heading. That's gonna be covered in the next section though!

We now have a solid foundation to implement the animation!

Let's think about how the animation should look from a visual standpoint.

When the page loads, what colors should all the headings be? It should probably look like this, where the first heading has the gradient background and the rest have white backgrounds πŸ‘‡πŸ»

Animated.

Gradient.

Text.



Then, after a little while, the first gradient should start fading away and the second one should start fading in. πŸ‘‡πŸ»

Animated.

Gradient.

Text.



Finally, the first line should be white and the second should have the gradient. πŸ‘‡πŸ»

Animated.

Gradient.

Text.


The same applies to the third line. Once that's finished, the first line's gradient appears again and the cycle restarts.

You can use this widget with an interactive slider to see how the gradients are fading in and out as you move the slider πŸ‘‡πŸ»

Animated.

Gradient.

Text.



To make the animation, we want to divide it into 6 parts. In each part, either

  • one heading will have the gradient background (the rest will have white background)
  • two headings will be fading in/out

We can use the table below to know what exact behavior we want to model in CSS:

PartHeading 1Heading 2Heading 3
1GradientNo gradientNo gradient
2Fading outFading inNo gradient
3No gradientGradientNo gradient
4No gradientFading outFading in
5No gradientNo gradientGradient
6Fading inNo gradientFading out

We have now done most of the hard work. Now it's time to translate the table above into CSS.

The key thing we'll be animating is the opacity of the gradient background.

When we want the gradient to be visible, we'll set it to 1 and when we want it to be invisible, we'll set it to 0.

Before we can do that, however, we need to make sure that there is a white background underneath the gradient.

To do that, we'll need to wrap the heading lines in span elements, add class heading-line-gradient to them and leverage the ::before pseudo-element.

<h1>
  <span class="heading-line heading-line-first">
    <span class="heading-line-gradient">Animated.</span>
  </span>
  <span class="heading-line heading-line-second">
    <span class="heading-line-gradient">Gradient.</span>
  </span>
  <span class="heading-line heading-line-third">
    <span class="heading-line-gradient">Text.</span>
  </span>
</h1>

The .heading-line-gradient will have the gradient background and .heading-line::before will have the white background.

/* move these style from .heading-line 
   to .heading-line-gradient */
.heading-line-gradient {
  color: transparent;
  -webkit-background-clip: text;
  background-clip: text;
}

/* move this from .heading-line-first */
.heading-line-first > .heading-line-gradient {
  background-image: linear-gradient(to left, #ed8936, #ed64a6);
}

/* move this from .heading-line-second */
.heading-line-second > .heading-line-gradient {
  background-image: linear-gradient(to left, #38b2ac, #0bc5ea);
}

/* move this from .heading-line-third */
.heading-line-third > .heading-line-gradient {
  background-image: linear-gradient(to left, #ecc94b, #48bb78);
}

/* white text underneath the gradient text */
.heading-line::before {
  line-height: 1;
  display: block;
  position: absolute;
  color: #fff;
  pointer-events: none;
  width: 100%;
}

.heading-line-first::before {
  content: "Animated.";
}

.heading-line-second::before {
  content: "Gradient.";
}

.heading-line-third::before {
  content: "Text.";
}

Finally, let's focus on the animation! We'll start with the first heading. We'll be animating the opacity of the heading-line-gradient class.

To define the animation, we'll use the @keyframes rule and the animation property to apply it.

We'll set the animation duration to 8s and set infinite as the number of iterations. πŸ‘‡πŸ»

@keyframes first_heading_gradient {
  0%,
  /* 16.667 = 1/6 */
  16.667% {
    opacity: 1;
  }

  /* 33.333% = 2/6 */
  33.333%,
  /* 83.333% = 5/6 */
  83.333% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}

.heading-line-first > .heading-line-gradient {
  /* ... */
  animation: first_heading_gradient 8s infinite;
}

We'll do the same for the second and third headings. πŸ‘‡πŸ»

@keyframes second_heading_gradient {
  0%,
  16.667% {
    opacity: 0;
  }

  33.333%,
  50% {
    opacity: 1;
  }

  66.667%,
  100% {
    opacity: 0;
  }
}

@keyframes third_heading_gradient {
  0%,
  50% {
    opacity: 0;
  }

  66.667%,
  83.333% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

.heading-line-second > .heading-line-gradient {
  /* ... */
  animation: second_heading_gradient 8s infinite;
}

.heading-line-third > .heading-line-gradient {
  /* ... */
  animation: third_heading_gradient 8s infinite;
}

This is what you should end up with πŸ‘‡πŸ»

Animated.

Gradient.

Text.

Animating white background

To make the animation stand out, we'll have to animate the white background of the headings as well.

Take one more look the what we want to achieve πŸ‘‡πŸ»

Animated.

Gradient.

Text.


You can notice that the white background is sort of fading in when the gradient is fading out and vice versa.

The effect is subtle, but it gives the animation a little bit more spice.

We can't, however, animate the white background for the whole duration of the gradient animation. That would look odd as the text would be almost invisible at one point.

Instead, we'll only start fading the white background away when the gradient is halfway through its fade-in animation and vice versa.

Play around with this widget to see how the opacity of the gradient and white background should behave πŸ‘‡πŸ»

Animated.

White text:

opacity: 0.00

Gradient text:

opacity: 1.00


Now that we have a better understanding of when to animate the white background, let's create a table that we'll later translate to CSS.

This table only deals with the first heading but the same logic applies to the second and third headings as well.

Now that we're animating the white background as well, we'll need to split the animation into 12 steps (instead of 6).

PartGradient opacityWhite background opacity
110
21 (start to fade out)0 (start to fade in)
30.5 (still fading out)1 (already faded in)
40 (faded out)1
501
601
701
801
901
100 (start fading in)1
110.5 (halfway faded in)1 (start fading out)
121 (faded in)0 (faded out)

I know that this table seems to be a bit overwhelming and a bit tedious to put together.

You can (and I highly encourage you to) create analogical tables for animating the second and third headings.

Anyways, we have done a lot of hard work, the last bit remaining is to express this table in CSS.

Let's start with the first heading. πŸ‘‡πŸ»

@keyframes first_heading_white_text {
  0%,
  /* 16.667 = 2/12 */
  16.667% {
    opacity: 0;
  }

  /* 25% = 3/12 */
  25%,
  /* 16.667 = 9/12 */
  91.667% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

.heading-line-first::before {
  /* ... */
  animation: first_heading_white_text 8s infinite;
}

Analogically, we want to animate the second and third headings' white backgrounds. πŸ‘‡πŸ»

@keyframes second_heading_white_text {
  0%,
  25% {
    opacity: 1;
  }

  33.333%,
  50% {
    opacity: 0;
  }

  58.333%,
  100% {
    opacity: 1;
  }
}

@keyframes third_heading_white_text {
  0%,
  58.333% {
    opacity: 1;
  }

  66.667%,
  83.333% {
    opacity: 0;
  }

  91.667%,
  100% {
    opacity: 1;
  }
}

.heading-line-second::before {
  /* ... */
  animation: second_heading_white_text 8s infinite;
}

.heading-line-third::before {
  /* ... */
  animation: third_heading_white_text 8s infinite;
}

πŸŽ‰πŸ₯³ We are DONE!!! πŸ₯³πŸŽ‰

Animated.

Gradient.

Text.

Source code

You can find the source code for this tutorial on CodeSandbox.

If you liked this tutorial, you can also check out and subscribe to my YouTube channel. Or/and subscribe to my newsletter to get notified when I publish new articles! πŸ‘‡

Subscribe to the newsletter

Stay up to date with state-of-the-art frontend development.

No spam. Unsubscribe any time.

hey@tomdohnal.com