CSS Background Animations
Background animations are an awesome touch when used correctly. In the past, I used MooTools to animate a background position. Luckily these days CSS animations are widely supported enough to rely on them to take over JavaScript-based animation tasks. The following simple CSS snippet animates the background image (via background position) of a given element.
The CSS
We'll use CSS animations instead of transitions for this effect:
@keyframes animatedBackground {
from { background-position: 0 0; }
to { background-position: 100% 0; }
}
With the animation code in place, now it's time to apply it to an element with a background image:
#animate-area {
width: 560px;
height: 400px;
background-image: url(bg-clouds.png);
background-position: 0px 0px;
background-repeat: repeat-x;
animation: animatedBackground 40s linear infinite;
}
The cloud background image within the sample element will elegantly scroll from left to right over a duration of 40 seconds, seamlessly repeating an infinite number of times.
How epic is it that we don't need to use JavaScript to manage these animations anymore? Of course the mess of vendor prefixes to accomplish the animation sucks, but at least the animations are much more efficient and more easily configurable!





May I suggest to use multiple background images to create a cool parallax effect in that cloud background?
Easy and efficient! Thanks for this css trick!
wow .. very awesome animation friend ,,,,,,,,, i love it .
Wow, great post and very thorough. Out of interest, where did you get the cloud image from? I did a similar tutorial on my blog about using CSS sprites to create a basic button. I guess using the same idea you could even animate the background of the button.
As MaxArt suggested, having multiple (small in file-size) backgrounds could create a cool effect. So I experimented! Check it out: http://www.creativewebgroup.co.uk/library/background.html
As MaxArt suggested, having multiple (small in file-size) backgrounds could create a cool effect. So I experimented!
Check it out: http://www.creativewebgroup.co.uk/library/background.html