Animated Gradient Text Color 🌈
Tags: webdev, showdev, css, tutorial
Published: 2022-01-04 12:59:47

We can make text more colorful using CSS gradients as background images, applied to text using blend modes, without using actual [color fonts](https://demagsign.io/will-color-fonts-unlock-a-new-level-of-expression-for-web-typography/). 

![Animated Rainbox Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d79ykhw53prr8bkapt48.gif)

In this tutorial, we will use rainbow colors to rebuild the effect step by step, with code snippets and demos that you can use to experiment on code pen.

I have used the same technique, based on a very old [tutorial on CSS-tricks.com](https://css-tricks.com/snippets/css/gradient-text/),  for my [portfolio relaunch](https://dev.to/ingosteinke/creating-a-fast-and-beautiful-portfolio-website-using-html-css-and-eleventy-and-netlify-9f5) in a more subtle way, but let's proceed with contrasting rainbow colors to add a vintage retro touch to our demo!

## CSS gradients as background images

Color gradients are a gradual blending from one color to another, or a series of blending using multiple colors like the colors of the rainbow. Gradients are treated as images in CSS.

## Talking about a Rainbow

We can't write: `color: rainbow` in CSS, but can still use words to describe certain values in CSS. While numbers allow for very precise values, words are easy to read and easy to keep in mind, so they are ideal for teaching, testing, and demonstration.

We can also specify the gradient direction in natural language by adding (for example) "to right" as a first value:

```css
.gradient-container {
  background: linear-gradient(
    to right,
    red, orange, yellow, green, blue, indigo, violet
  );
}
```

Depending on your editor (and [extension](https://github.com/kamikillerto/vscode-colorize.git), you can even see the rainbox colors in the syntax highlighting of your code.

![Screenshot of our codepen and a colorful syntax highlighting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsrfhad7t2gekua83f4p.png)

But an actual rainbow is a bow, not a bar, so let's change the linear gradient to a radial one.

### A radial gradient...

Gradients can have different shapes and directions, which are more generally called types of gradients. We can't define free-form polygons, but we can combine basic gradient types in CSS, see the [overview: using CSS gradients at Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Images/Using_CSS_gradients).

Changing "linear" to "radial" and setting equal width and height displays a colorful box. But that's far from looking like a rainbow yet!

[![Codepen: a radial gradient, but not a rainbow yet!](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/skzvwoucdfxa8i6e0b4f.png)](https://codepen.io/openmindculture/pen/NWaMRxW)

## ... turns into a rainbow gradient

Let's add white at each end of the gradient, and set the position. Like we used "to right", we can say "at 0% 100%" to put the center bottom left.
Let's also add [color-stops](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Images/Using_CSS_gradients#positioning_color_stops) to enlarge the white areas:

```css
.gradient-container {
  background: radial-gradient(
    at 0% 100%, /* position center to bottom left */
    to right,
    white 20%, /* color-stop to enlarge white center */
    red, orange, yellow, green, blue, indigo, violet,
    white 50%  /* color-stop to enlarge white outside */
  );
}
```

[![Rainbow gradient codepen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0dfum3rj5603kejubq3.png)](https://codepen.io/openmindculture/pen/VwMxKMV)

## Rainbow Letters

But what if we want to apply rainbow colors to a text?

```css
.rainbowtext {
  color: rainbow; /* (invalid) */
```

Text can have color, but gradients are images (not colors) in CSS, so we can only use them as background-images. But like this [2010 CSS tricks tutorial](https://css-tricks.com/snippets/css/gradient-text/) shows, a combination of `webkit-background-clip` and `webkit-text-fill-color` makes browsers (including Chrome) apply the background to the text:

```
-webkit-text-fill-color: transparent;
-webkit-background-clip: text;
```

Both `webkit-text-fill-color` and `background-clip` (without vendor prefix) are supported by all major browsers except Internet Explorer in 2022), but we can still use a static font color as a fallback and make sure it doesn't look messed up. And we can set a smaller gradient size so that all colors are visible on the letters at once.

We will use a linear gradient again, so that every letter can have all of the colors (at least, as long the text is only on one line).

```css
.rainbowtext {
  color: red; /* fallback color */
  background: linear-gradient(red, orange, yellow, green, blue, indigo, violet);
  /* magic workarounds for modern browsers */
  -webkit-text-fill-color: transparent;
  background-clip: unset;
  -webkit-background-clip: text;
  /* make all colors visible at once */
  background-size: 100% 60%;
}
```

![Rainbox text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4f4l7rf2l9vc9zwj7h6.png)

## Animation

Finally, we can even animate our gradient...

```css
.rainbowtext { /* ... */
  animation: AnimateTextGradient 7s ease infinite;
}

@keyframes AnimateTextGradient {
  0% { background-position: 50% 0; }
  50% { background-position: 50% 100%; }
  100% { background-position: 50% 0; }
}
```

[![Codepen with animated rainbow text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjl3ne5j72mkko9seago.png)](https://codepen.io/openmindculture/pen/abLGdLY)

Check this codepen to see it in action!

{% codepen https://codepen.io/openmindculture/pen/abLGdLY %}

## What's next in CSS?

I will share some more examples how to make use of new and underrated CSS features in this DEV blog series: