CSS3 Tricks: Falling Leaves with CSS only
In today’s tutorial we will create Falling leaves effect without any help of javascript. When I started playing with CSS3 I never thought that I could make such fascinating animations with it. I made falling leaves animation in both Flash (used in our WordPress Falling Leaves plugin) and Javascript but I never thought that it is possible to make it with CSS3 only. While it is not as cool as the Flash one, it is still fairly decent and it uses less CPU. I recommend that you download the source files first and check the live demo. If you’d like to print these instructions as a reference, ordering new ink online is the easiest route – the best ink providers also offer toner cartridge recycling free of charge to all customers.
LIVE DEMO
Download Falling Leaves with CSS only source (846)
SETTING UP THE LEAVES
First we have to add the class for leaf in our html file. Simply add the following code into index.html:
We add one instance of the leaf with the <span></span> tag. If you wish to add more leaves simply multiply this tag. Now create a file style.css and add the design for the class created above:
display: inline-block;
width: 80px;
height: 80px;
margin: -280px 40px 54px -34px;
background:url("leaf.png");
-webkit-animation: fallingLeaves 10s infinite linear;
-moz-animation: fallingLeaves 10s infinite linear;
}
Add the leaf here as a background for each instance of span that we defined earlier. Size and initial position of the leaves is set here. Webkit animation is actually pretty simple. We set which class to animate (fallingLeaves) and set the falling time to 10 seconds. We want the animation to continuously repeat itself so we will set it to infinite. Eeasing is set to linear to achieve a smooth falling effect.
Before we get to the animation effect we have to somehow randomize the leaves so they don’t all start falling at the same time. I used the nth-child method to define several sequences of leaves with different animation delays.
-webkit-animation-delay: 1s;
-moz-animation-delay: 1s;
}
.fallingLeaves span:nth-child(3n+2) {
-webkit-animation-delay: 1.5s;
-moz-animation-delay: 1.5s;
}
.fallingLeaves span:nth-child(2n+5) {
-webkit-animation-delay: 1.7s;
-moz-animation-delay: 1.7s;
}
These are just three examples, I added several more sequences in the live example provided above. Download the source files to see them all. First sequence is set to 5n+5 which means leaves number 5, 10, 15,… will start falling with the delay of 1 second. Second sequence is set to 3n + 2 which means leaves 2,5, 8, 11,… will start falling with the delay of 1.5 second. Note that some sequences may overlap with each other but it’s not a big deal, the css will always take the last setting and use it for the animation.
SETTING UP THE ANIMATION
All we have to add now is the animation effect. It is actually pretty simple. Add the following code to your style.css file:
0% {
opacity: 1;
-webkit-transform: translate(0, 0px) rotateZ(0deg);
}
75% {
opacity: 1;
-webkit-transform: translate(100px, 600px) rotateZ(270deg);
}
100% {
opacity: 0;
-webkit-transform: translate(150px, 800px) rotateZ(360deg);
}
}
As you can see we divided our animation into three parts (state at 0%, at 75% and at 100%). Animation starts with x and y coordinates set to 0px and rotation along Z axis also set to 0. This is the initial position of the animation. After the animation starts it will start moving towards position set in the second part (75%). Leaves will move slightly to the right (x coordinate is set to 100px) and down (y coordinate is set to 600px). It will also rotate for 3/4 of the full circle (270deg). Leaves will still be fully visible (opacity is set to 1). Opacity is the reason why we added the second part of animation anyway. It allows us to slowly fade the leaves to invisible after they reach a certain point. When leaves reach the ending point (100%) they are instantly moved back up where they start animating again. And we are done. The implementation is pretty simple and straightforward and you get a decent falling leaves effect in no time.
CONCLUSION
CSS3 animations can be very powerful and diverse and even animation of multiple objects is a piece of cake. Please note that these animations are only supported in webkit browsers (Chrome, Opera). In our future tips&tricks posts I will show you how to make gradients, apply animations to images and much more so stay tuned!
Tags: css3 animation, css3 falling leaves, css3 tricks








14 Comments for CSS3 Tricks: Falling Leaves with CSS only
January 22, 2012 at 9:03 am
contradictory to this:
“supported in webkit browsers (Chrome, Opera, latest versions of FF).”
it doesn’t work with FF 9,0.1 (that’s the latest version)
January 23, 2012 at 5:56 am
Hi,
Yes you are right. I have corrected the post.
best regards.
January 23, 2012 at 7:02 am
that is so awesome. I’m gonna use that for a Christmas card this year with falling snowflakes. sure eats animated gifs in PShop!
January 23, 2012 at 9:24 am
Such a useful script to make fun with CSS. Great to know some very nice tips about it. Thanks
January 24, 2012 at 4:07 am
I think there is an error code in the demo?
Here @-moz-keyframes loader
why use -webkit-transform
!!!
nice trick
April 26, 2012 at 9:53 am
You are right… silly mistake in the code, FireFox supports all this stuff.
I created a Dabblet and fixed the mistake and it works in FireFox:
http://dabblet.com/gist/2498319
January 25, 2012 at 11:27 am
Whoa, brilliant giveaway. Nice to use this falling leaves script which i am searching for many days. Good one.
January 26, 2012 at 3:08 am
Nice effect. I would worry about cross-browser compatibility though.
January 26, 2012 at 6:51 am
Hi,
Yes, this tutorial is simply for showing what can be achieved with CSS3 and only works in webkit browsers like Chrome. But support for this kind of effects will ofcourse increase in the future.
best regards,
Team PremiumCoding
February 1, 2012 at 3:54 pm
does it work on IE??
February 2, 2012 at 6:37 am
Hi,
No, IE doesn’t support this kind of CSS3, only webkit browsers do.
best regards,
Team PremiumCoding
April 24, 2012 at 4:16 pm
didnt work for me, only saw a backyard with canoe and some trees, no falling trees.
This type of site should explain how to install webkit.
I think webkit will make it a javascript thing, so saying it is CSS, may be a mis-conception here.
April 24, 2012 at 6:58 pm
Hi,
Not really, no need to install anything. It depends on the browser, Chrome for instance is a webkit browser.
Best regards.
July 20, 2012 at 1:35 am
Thank you so much for posting this. I’ve been looking for such an animation for days now. Your page rank should be much higher than it is. I’ll be sure to link to this on my page to help with that.
With that said, is there a solution for the fluctuation in screen size and induction of the scroll bars?
Thanx again.