In this quick tip, we’ll show how easy it is to animate a background gradient with CSS. In a recent article, we showed how to set a background gradient on text. The CodePen demo below shows the result. Make sure to read through that article if you’re not sure how we got to this result, as we’ll build on this example below. For the sake of this demo, let’s add in some extra colors to our gradient background:
background-image: linear-gradient( 45deg, #ffb3ba, #c49c6e, #bfbf76, #77b084, #ff7e74, #3b82f6, #c084fc, #db2777 );
If we turn off background-clip: text and text-color: transparent for a moment, we get a better sense of how our gradient fills the text’s content box. Let’s now go through the steps of animating our background gradient.
Step 1: Adjusting the Background Size
To animate our gradient background, we need to make it bigger than the text’s content box so we can’t see all of it at once. We can do that with the handy background-size property. (You can read all about background-size here.) I’m going to set the width of our background gradient to three times the width of our heading element:
background-size: 300% 100%;
Now, only a third of the gradient background will be visible at any one time, as seen below.
Step 2: Setting an Animation
Next, we’ll set up an animation that will move the background around so that we’ll see different parts of it over time. We can set up a simple animation rule like so:
animation: gradientAnimation 8s linear infinite;
That will move the background back and forth once every 16 seconds. Next, we’ll set up an @keyframes statement:
@keyframes gradientAnimation {
0% { background-position: 0; }
to { background-position: 100%; }
}
This simple @keyframes statement will move our background from the top left to the bottom right every eight seconds. In the Pen below, we’ve once again disabled background-clip: text and color: transparent so we can see what’s happening in the background. Once we re-enable background-clip: text and color: transparent, we see the finished product. Pretty cool!
Animating a Background Image
In our article on adding gradient effects and patterns to text, we also used a floral background image. (See the Pen for that here.) Let’s have a go at animating that background too. We’ll do it slightly differently, as we don’t want to distort the background image. Let’s remove background-size: contain from the original demo and not set a background size at all. That leaves us with this:
color: transparent;
-webkit-background-clip: text;
background-clip: text;
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 is shown in the CodePen demo below. Try turning off background-clip: text and text-color: transparent for a moment if you want to see what’s happening in the background. Our background image is repeating by default, which doesn’t look too bad for this particular image. (Just out of interest, try adding background-repeat: no-repeat to see what what happens without a repeating background.) In other situations, where the background image doesn’t tile so well, you might want to prevent the image repeating and then use background-size to make the image larger, like we did with the gradient background above. For example:
background-repeat: no-repeat;
background-size: 120%;
Here’s the effect of doing that on our floral demo.
Conclusion
We could do much more spectacular animations that this, but the aim was to keep it simple here. You can dig deeper into CSS keyframes and animations in How to Get Started with CSS Animation. You can also play around with the timing of the animation, angle of the gradient and so on. As mentioned in the previous article, have fun with this but don’t go overboard, as too much of this kind of animation can become annoying. A subtle animated background on a heading might just add that touch of interest or intrigue you need to keep your audience engaged.
Source link