O'Reilly

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!

Track.js Error Reporting

Upcoming Events

Recent Features

  • Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

  • 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

  • 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...

  • MooTools: Set Style Per Media

    I'd bet one of the most used MooTools methods is the setStyle() method, which allows you to set CSS style declarations for an element. One of the limitations of MooTools' setStyle() method is that it sets the specific style for all medias. What if...

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. I’ve modeled my code after http://davidwalsh.name/multiple-background-css-animations (demo http://davidwalsh.name/demo/multiple-background-css-animations.php).

    Here’s my adaptation (I’ll also paste it below): http://jsbin.com/cifig/1/

    The goal is to create an infinitely scrolling, two-layer parallax background. The effect works perfectly in Mac Chrome and Safari, but it stutters in Firefox.

    Any ideas why David Walsh’s version works in all three browsers, but mine fails? Thanks!

      body {
        background-color: black;
      }
    
      #container {
        position: absolute;
        top: 0; right: 0; bottom: 0; left: 0;
      }
    
      @-webkit-keyframes scrollBg {
        100% { background-position: 0 0; }
      }
    
      @keyframes scrollBg {
        100% { background-position: 0 0; }
      }
    
      .shotsBg1 {
        width: 100%;
        height: 100%;
        position: fixed;
        background-image: url(http://shotseverybody.com/images/shot_tile.png);
        background-size: 128px 156px;
        background-position: 0 -156px;
        opacity: 0.5;
        -webkit-animation: scrollBg 2.5s linear infinite;
        animation: scrollBg 2.5s linear infinite;
      }
    
      .shotsBg2 {
        width: 100%;
        height: 100%;
        position: fixed;
        background-image: url(http://shotseverybody.com/images/shot_tile.png);
        background-size: 64px 78px;
        background-position: 0 -78px;
        opacity: 0.25;
        -webkit-animation: scrollBg 5s linear infinite;
        animation: scrollBg 5s linear infinite;
      }
    

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

Recently on David Walsh Blog

  • OâReilly Velocity Conference â New York

    My favorite front-end conference has always been O'Reilly's Velocity Conference because the conference series has focused on one of the most undervalued parts of client side coding:  speed.  So often we're so excited that our JavaScript works that we forget that speed, efficiency, and performance are just as important. The next Velocity...

  • Free Download: Font Bundle Featuring 17 Incredible Typefaces

    The only thing we love more than a good font, is a good free font. So we’ve combed the Web for some of our favorite free fonts, and gathered them here in a single download. You’ll find a variety of useful typefaces, from highly geometric designs...

  • OâReilly Velocity Conference â Amsterdam

    My favorite front-end conference has always been O'Reilly's Velocity Conference because the conference series has focused on one of the most undervalued parts of client side coding:  speed.  So often we're so excited that our JavaScript works that we forget that speed, efficiency, and performance are just as important. The next Velocity...

  • CanIUse Command Line

    Every front-end developer should be well acquainted with CanIUse, the website that lets you view browser support for browser features.  When people criticize my blog posts for not detailing browser support for features within the post, I tell them to check CanIUse:  always up to date, unlike...

  • Generating Alternative Stylesheets for Browsers Without @media

    If your CSS code is built with a mobile-first approach, it probably contains all the rules that make up the "desktop" view inside @media statements. That's great, but browsers that don't support media queries (IE 8 and below) will simply ignore them, ending up getting the...