Code Snippet
WebKit Keyframe Animation Syntax
Basic Declaration & Usage
@-webkit-keyframes NAME-YOUR-ANIMATION {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}#box {
-webkit-animation: NAME-YOUR-ANIMATION 5s infinite;
}Multiple steps
@-webkit-keyframes fontbulger {
0% {
font-size: 10px;
}
30% {
font-size: 15px;
}
100% {
font-size: 12px;
}
}
#box {
-webkit-animation: fontbulger 2s infinite;
}If an animation has the same starting and ending properties, one way to do that is to comma-separate the 0% and 100% values:
@-webkit-keyframes fontbulger {
0%, 100% {
font-size: 10px;
}
50% {
font-size: 12px;
}
}Or, you could always tell the animation to run twice (or any even number of times) and tell the direction to alternate.
Calling keyframe animation with separate properties
#box {
-webkit-animation-name: bounce;
-webkit-animation-duration: 4s;
-webkit-animation-iteration-count: 10;
-webkit-animation-direction: alternate;
-webkit-animation-timing-function: ease-out;
-webkit-animation-fill-mode: forwards;
-webkit-animation-delay: 2s;
}| timing-function | ease, ease-out, ease-in, ease-in-out, linear, cubic-bezier(x1, y1, x2, y2) (e.g. cubic-bezier(0.5, 0.2, 0.3, 1.0)) |
| duration & delay | Xs or Xms |
| duration-count | X |
| fill-mode | forwards, backwards, both, none |
| animation-direction | normal, alternate |
Animation Shorthand
Just space-separate all the individual values. The order doesn't matter except when using both duration and delay, they need to be in that order. In the example below 1s = duration, 2s = delay, 3 = interations.
-webkit-animation: test 1s 2s 3 alternate backwardsCombine transform and animation
@-webkit-keyframes infinite-spinning {
from {
-webkit-transform: rotate(0deg);
}
to {
-webkit-transform: rotate(360deg);
}
}Multiple animations
You can comma-separate the values to delcare multiple animations on a selector.
.animate-this {
-webkit-animation:
first-animation 2s infinite,
another-animation 1s;
}
is from & to equal to using 0% and 100%?
Yep! It is.
Hello Chris Coyier,
The tips and tricks are nice, but if you include the demo link then it would be much helpful. For audience to view the effect live and you too to increase potential returning traffic.
Guys – this is the snippets section! It’s literally the only section of the site that doesn’t have demos/tutorials, it’s purely the grab-n-go snippets… As described!
I agree with Hiren. A demo attached to the snippets (whenever possible) would be very nice.
Agreed on the demo attached to snippets. Just something super-simple.
Hi Chris, you can use -webkit-animation-delay to delay the effects.
hey, Chris, the delay can be found here:
http://css-infos.net/property/-webkit-animation-delay
you can also use shorthand to set multiple declarations in one step:
http://css-infos.net/property/-webkit-animation
cheers!
Atg
Just experimenting, I found the webkit delay syntax is:
“-webkit-animation-delay: 5s;”
Pretty simple!
Hi and many thanks for the code snippet. Can anyone please help me with the following opacity animation:
The div layer starts invisible it then animates to fully visible (after a 2second delay) and remains in that state.
Currently with the code above (including the delay code) I can only get the following:
The div layer starts visible it then animates (flashes invisible then animates) to fully visible (after a 2second delay) and remains in that state.
If I put an opacity: 0; on the div then the following occurs:
The div layer starts invisible it then animates to fully visible (after a 2second delay) and then returns to invisible.
Is what I am attempting even possible or am I just being a Muppet?
many thanks.
Hey Spence -
I think what you’re looking for is only possible on hover/state change. This is how you do it:
1. Put your animate attributes on the element(s) you want to animate, ie:
.box {
-webkit-transition: all 0.2s ease-in-out;
}
2. On the hover:
.box:hover {
background: red;
}
That’s it! Change as necessary.
Adam, doesn’t the inability to do what Spence is looking for sort of defeats the purpose of animating in this way?
I’m encountering exactly the same problem. Any further thoughts on how to resolve it without hover approach?
Hi Spence,
I was just scratching my head about the same question and came across this page:
http://css3animator.com/2010/12/how-to-control-your-css3-animations/
I think this is what you’re looking for, you want to use forwards to make the last keyframe of your animation persist so you can make it look as if something has appeared
I also discovered if you use from and to, this behaviour is the default but it you want to use several keyframed states then it will default back to its original state unless you state the fill-mode
hth.
m.
Just use -webkit-animation-iteration-count: 1;
nice!
thanks Mairead
cool tut
thank you
Hey Chris! Really informative snippet! You’re blog just keeps getting better and better =)
Anyway, you mistakenly repeated ‘has the same’ in “has the same starting…”
Thanks!
Thanks, fixed!
Hi there all!
I’m new to CSS and a bit stuck! I have “2″ images 70px By 70px I want “1″ to stay still and “2″ to rotate. image “2″ has just a little dot and image “1″ has an inner circle and an outer circle.. So I want the little dot to rotate between the inner and outer circle.. So far I’ve done it but the dot makes a big rotation off the screen!! I don’t no if it’s the stage of my rotation or not using margin’s the right way or the perspective origin not being there or non of the above. Thanks for any help in advance and hope someone knows what I’m going on about
This is really nice! Thanks guys for sharing this. I’m going to try it now :)
I’ve lost count how many times I’ve come back to this page. A great, simple writeup.