The way to Animate Textual content Gradients and Patterns in CSS — SitePoint

The way to Animate Textual content Gradients and Patterns in CSS — SitePoint
The way to Animate Textual content Gradients and Patterns in CSS — SitePoint


On this fast tip, we’ll present how simple it’s to animate a background gradient with CSS.

In a latest article, we confirmed how to set a background gradient on text. The CodePen demo beneath reveals the end result.

Be sure to learn by that article in case you’re undecided how we received to this end result, as we’ll construct on this instance beneath.

For the sake of this demo, let’s add in some further colours to our gradient background:

h1 {
  background-image: linear-gradient(
    45deg,
    #ffb3ba,
    #c49c6e,
    #bfbf76,
    #77b084,
    #ff7e74,
    #3b82f6,
    #c084fc,
    #db2777
   );
}

If we flip off background-clip: textual content and text-color: clear for a second, we get a greater sense of how our gradient fills the textual content’s content material field.

Let’s now undergo the steps of animating our background gradient.

Step 1: Adjusting the Background Dimension

To animate our gradient background, we have to make it greater than the textual content’s content material field so we will’t see all of it directly. We will try this with the useful background-size property. (You possibly can read all about background-size here.)

I’m going to set the width of our background gradient to 3 occasions the width of our heading component:

h1 {
  background-size: 300% 100%;
}

Now, solely a 3rd of the gradient background will probably be seen at anybody time, as seen beneath.

Step 2: Setting an Animation

Subsequent, we’ll arrange an animation that can transfer the background round in order that we’ll see totally different elements of it over time.

We will arrange a easy animation rule like so:

h1 {
  animation: gradientAnimation 8s linear infinite;
}

That can transfer the background forwards and backwards as soon as each 16 seconds.

Subsequent, we’ll arrange an @keyframes assertion:

@keyframes gradientAnimation {
  0% {
    background-position: 0;
  }
  to {
    background-position: 100%;
  }
}

This easy @keyframes assertion will transfer our background from the highest left to the underside proper each eight seconds.

Within the Pen beneath, we’ve as soon as once more disabled background-clip: textual content and shade: clear so we will see what’s occurring within the background.

As soon as we re-enable background-clip: textual content and shade: clear, we see the completed product.

Fairly cool!

Animating a Background Picture

In our article on including gradient results and patterns to textual content, we additionally used a floral background picture. (See the Pen for that here.)

Let’s have a go at animating that background too. We’ll do it barely in a different way, as we don’t need to distort the background picture.

Let’s take away background-size: include from the unique demo and never set a background measurement in any respect. That leaves us with this:

h1 {
  shade: clear;
  -webkit-background-clip: textual content; 
  background-clip: textual content; 
  background-image: url(floral.jpg);
  -webkit-text-stroke: 1px #7512d7;
  text-stroke: 1px #7512d7;
  animation: gradientAnimation 20s linear infinite;
  animation-direction: alternate;
}

@keyframes gradientAnimation {
  0% {
    background-position: 0;
  }
  to {
    background-position: 100%;
  }
}

The result’s proven within the CodePen demo beneath.

Attempt turning off background-clip: textual content and text-color: clear for a second if you wish to see what’s occurring within the background.

Our background picture is repeating by default, which doesn’t look too unhealthy for this specific picture. (Simply out of curiosity, strive including background-repeat: no-repeat to see what what occurs and not using a repeating background.) In different conditions, the place the background picture doesn’t tile so nicely, you would possibly need to forestall the picture repeating after which use background-size to make the picture bigger, like we did with the gradient background above. For instance:

h1 {
  background-repeat: no-repeat;
  background-size: 120%;
}

Right here’s the impact of doing that on our floral demo.

Conclusion

We may do far more spectacular animations that this, however the goal was to maintain it easy right here. You possibly can dig deeper into CSS keyframes and animations in How to Get Started with CSS Animation. You can even mess around with the timing of the animation, angle of the gradient and so forth.

As talked about within the earlier article, have enjoyable with this however don’t go overboard, as an excessive amount of of this type of animation can change into annoying. A refined animated background on a heading would possibly simply add that contact of curiosity or intrigue you should preserve your viewers engaged.

Leave a Reply

Your email address will not be published. Required fields are marked *