Multiple Background CSS Animations

By  on  

CSS background animation has been a hot topic for a long time, mostly because they look pretty sweet and don't require additional elements.  I was recently asked if it was possible to have multiple background animations on a given element and the answer is yes...with concessions.  Let's take a look at how it's done!

The CSS

So multiple background images on an element is something we've been able to do for quite a while now, simply separate them with commas:

.animate-area	{ 
	background-image: url(twitter-logo-bird.png), url(treehouseFrog.png), url(bg-clouds.png);
	background-position: 20px -90px, 30px 80px, 0px 0px;
	background-repeat: no-repeat, no-repeat, repeat-x;
}

Note that the background image that you want at the top of the stack should go first in the image list.  Animating the backgrounds requires moving the background-position, also separated by commas:

@keyframes animatedBird {
	from { background-position: 20px 20px, 30px 80px, 0 0; }
	to { background-position: 300px -90px, 30px 20px, 100% 0; }
}
		
.animate-area	{ 
	animation: animatedBird 4s linear infinite;
}

The result is three moving pieces inside one element!

Of course this isn't an ideal case because you can't separately change background-positions and thus you need to work with the same duration for each background image.  Multiple animations can be set on selectors with CSS, but since it's the one property being changed, we're just out of luck!

Recent Features

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    Create a Dynamic Flickr Image Search with the Dojo Toolkit

    The Dojo Toolkit is a treasure chest of great JavaScript classes.  You can find basic JavaScript functionality classes for AJAX, node manipulation, animations, and the like within Dojo.  You can find elegant, functional UI widgets like DropDown Menus, tabbed interfaces, and form element replacements within...

  • By
    Create a Context Menu with Dojo and Dijit

    Context menus, used in the right type of web application, can be invaluable.  They provide shortcut methods to different functionality within the application and, with just a right click, they are readily available.  Dojo's Dijit frameworks provides an easy way to create stylish, flexible context...

Discussion

  1. looks great as usually,
    now ready to make another article to combine this stuff ;)

  2. Great Tutorial David. Just with a few lines of CSS codes to create such a beautiful animation.
    Thanks.

  3. Nice tutorial! I never thought that making animation using so easy. But the problem is it doesn’t work in old browsers! :(

  4. Mich

    @Kiash: It’s time to stop using old browsers

  5. Crispen Smith

    David, I wanted to thank you for an awesome article. You got me thinking thought about the limitations and working within them.

    It seems that all is not lost when it comes to “story telling” via this technique.

    I did up a quick pen to explore these limitations.

    http://codepen.io/Crispen_Smith/pen/HGsxw

  6. Hichem benchaaben

    Cool but , why you not incorporate a fallback option for non supported browser in your tutorials ?
    Like you begin first of all by saying hey guys, check which browser can do this : ……
    Here is what usually will look like ……
    Here is the polyfills which you should do ….

  7. If your only concern is the markup and you don’t mind more complex CSS, an alternative is using pseudo elements which allows for the same animations using different durations: http://codepen.io/bananascript/pen/sBlhm

  8. Jesse

    Hi, great stuff!
    I’ve got a problem though – is there any way of getting this to work with multiple background images if I’m using the new extra-specific background-position as so:

    background-position: top 25% left 100px, bottom 50px right: 10%;
    

    ?????
    I’m pulling my hair out because I need to animate a multiple background system where I’m using this four-value background-position. Is it actually possible?
    Thanks in advance!

  9. Jesse

    OK, please excuse me, I’ve just answered my own question. It works!
    Just so that you know, it is imperative that when you’re using specific position syntax (background-position: bottom 10% right 50px; for example), when animating you cannot swap from one side to another (for example, you cannot animate from “background-position: bottom 10% right 50px” to “background-position: bottom 10% left 50px”; both values of start and end would have to be bottom and right in this case, not bottom right and then bottom left).

    Example of my code (yes, I know, it’s insane…):

    @keyframes bgMov { from {background-position: bottom -30% left 0, bottom 40% left 2%, top 60% left 0, top 95% right 0, top 78% left 0, top 65% right 0, top 59% left 0, top 55% right 0, top left;} to {background-position: bottom -30% left 586px, bottom 40% left 2%, top 60% left 0, top 95% right 520px, top 78% left 400px, top 65% right 260px, top 59% left 180px, top 55% right 120px, top left;} }
    

    Thanks anyway!

  10. Sabith Pocker

    You are awesome!

  11. Hey, thank you! I would like to animate one background but one of the backgrounds I don’t want. And it’s not working to keep it with animation “none”;

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!