Keyframe Animation Syntax
Basic Declaration & Usage
@-webkit-keyframes NAME-YOUR-ANIMATION {
0% { opacity: 0; }
100% { opacity: 1; }
}
@-moz-keyframes NAME-YOUR-ANIMATION {
0% { opacity: 0; }
100% { opacity: 1; }
}
@-o-keyframes NAME-YOUR-ANIMATION {
0% { opacity: 0; }
100% { opacity: 1; }
}
@keyframes NAME-YOUR-ANIMATION {
0% { opacity: 0; }
100% { opacity: 1; }
}
#box {
-webkit-animation: NAME-YOUR-ANIMATION 5s infinite; /* Safari 4+ */
-moz-animation: NAME-YOUR-ANIMATION 5s infinite; /* Fx 5+ */
-o-animation: NAME-YOUR-ANIMATION 5s infinite; /* Opera 12+ */
animation: NAME-YOUR-ANIMATION 5s infinite; /* IE 10+ */
}
For the sake of brevity the rest of the code on this page will not use any prefixes, but real world usage should use all the vendor prefixes from above
Multiple steps
@keyframes fontbulger {
0% {
font-size: 10px;
}
30% {
font-size: 15px;
}
100% {
font-size: 12px;
}
}
#box {
-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:
@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 {
animation-name: bounce;
animation-duration: 4s;
animation-iteration-count: 10;
animation-direction: alternate;
animation-timing-function: ease-out;
animation-fill-mode: forwards;
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 = iterations.
-webkit-animation: test 1s 2s 3 alternate backwards
Combine transform and animation
@keyframes infinite-spinning {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
Multiple animations
You can comma-separate the values to declare multiple animations on a selector.
.animate-this {
animation:
first-animation 2s infinite,
another-animation 1s;
}
Steps()
The steps() function controls exactly how many keyframes will render in the animation timeframe. Let's say you declare:
@keyframes move {
from { top: 0; left: 0; }
to { top: 100px; left: 100px; }
}
If you use steps(10) in your animation, it will make sure only 10 keyframes happen in the allotted time.
.move {
animation: move 10s steps(10) infinite alternate;
}
The math works out nicely there. Every one second, the element will move 10px to the left and 10px down, until the end of the animation, and then again in reverse forever.
This can be great for spritesheet animation like this demo by simurai.
Browser Support
Firefox 5+, IE 10+, Chrome, Safari 4+, Opera 12+
is from & to equal to using 0% and 100%?
Yep! It is.
Yep..!!
great information i really enjoy it thankx so much
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!
Font Bulger:
http://jsfiddle.net/keif/p8ytP/
Infinite Spinning:
http://jsfiddle.net/keif/LTvLF/
Trust me, there is no better way to learn this stuff than to try and build your own demo.
This is even more true with JavaScript frameworks, and JSFiddle is an invaluable tool to throw down quick and dirty code to figure pieces out.
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
Hi Lee,
It looks like you have to define a different center for the rotation. When you use -webkit-transform: rotate(), you can define the center point for both 2D and 3D rotation with -webkit-transform-origin (http://css-infos.net/property/-webkit-transform-origin)
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.
Hello everybody!
What about overriding a @keyframe animation set? I can’t figure out if it’s possible (using chrome).
I explain myself:
I made a webapp with several CSS3 features and @keyframes named sets for UI to interact beautifully. My app is customizable by different connected users. The customization is made by importing a css file that override some colors and layouts. I want people how knows CSS3 awesomness to be able to override my standard animations (triggered via javascript). But it looks like if you write a second @keyframes with the same name than first’s, the second definition is ignored.
Any help with this?
Love it!
nice content but boring site colors, Older site is much better in terms of colors
thank u chris. love it.
I can not load this page in ie8. The loading time take more…
Hi guys I want to be able to control the “frames” so I have 5 frames inside of one CSS sprite. They represent each state. I want them to play out over a specified amount of time with out the even motion in between I just want it to go from one frame to another over a specified amount of time. It’s hard for me to explain but flash has a way to do this rather easily.
Hey Chris, I think you want to use the step-start property. I used it on this animation (http://codepen.io/ScottJH/full/IAyEp). I realize this comment is over a year old, but hey, thought I’d contribute if I could.
Hello Chris,
I found your website very helpful. I learnt & implemented lot of new ideas from your site.
I was thrilled when I learnt this specific animation trick – usage of keyframes. I have implemented this to highlight the Archives on my site ABAP Help when people hover on it.
Thanks again so much for all these tutorials.
Regards,
Naimesh Patel
I have a question about combining animations – when I try the below I get either ‘leaffall’ OR ‘drop’ randomly on refresh. I was expecting both to happen simultaneously.
Anyone seen this problem?
Thank you so much for this snippet, Chris.
Regards from Brazil!
This is great! I was staying away from CSS3 animations because I thought the syntax was too difficult, but you’ve made it pretty simple to understand. Thanks.
For the infinite loop you could change the “to” value to 359deg to prevent it stopping
Its awesome, very easy to understand and a great thank for giving such a wonderful tutorial. I want to know if i want an animation (eg: text animation -fading) on sentance after another how can I achieve it. We do similar kind of text animation when we create presentations.
“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 = iterations.”
This doesn’t work, if you set the number of iterations to infinite.
Instead infinite has to be at the end, hasn’t it?
Thanks for this useful snippets! :)
It should be noted that the keyframe sequence is effectively “reset” if the element is altered via jQuery or some other thing. Keyframes for cycling through border colors for a play button on a player gets reset everytime the button switches from “play” to “pause”. For instance:
@keyframes change_border
{
0% {border-color: #202424;}
25% {border-color: #a09b8c;}
75% {border-color: #69707d;}
100% {border-color: #737e88;}
}
div#player_control {
animation: change_border 66s infinite;
animation-direction:alternate;
-moz-animation: change_border 66s infinite;
-moz-animation-direction:alternate;
-webkit-animation: change_border 66s infinite;
-webkit-animation-direction:alternate;
}
This “infinite” sequence of color changing would be interrupted every time the #player_control layer was altered. This is undesirable when other things are also tied to this animation sequence, but aren’t subject to the interruption. Could also simply be undesirable.
Thank you for this script.
Works great :)
Thanks.
It seems that Android 2.2 doesn’t perform well if you specify more than two states in percentages:
a.) this doesn’t work
b.) this works
You can’t spot the problem if you change user agent in webkit browser (etc. safari) or using Android SDK Tools. Bug seems to appear just on particular Android 2.2 devices.
tested on:
HTC Wildfire
Mozilla/5.0 (Linux; U; Android 2.2.1; sv-se; HTC Wildfire Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
HTC Evo
Mozilla/5.0 (Linux; U; Android 2.2; en-us; Sprint APA9292KT Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
thanks “pirohy”
You have some of the best CSS tips. Your directions are often better (more detailed and advanced) than the W3 Schools I appreciate your quality posts Chris. Thank you.
Is there a way to point the animation to another div? Say you make one div like a button to make the other div do the action?
In this example im trying to make it when I hover on the black box the white box will move out of the way. I haven’t seen anything on triggering an animation of one div by hovering on another before, at least not with pure CSS.
Nevermind, I figured it out.
by putting .blackbox:hover and .whitebox in the same selector it acts just the way I want it too.
Awesome snippet by the way. Very helpful.
How do you set the animation parameters to be the parameters of the element from the moment the page loads.
e.g.
So what happens here is that the element will appear on screen, then after 2 seconds it will dissapear and run through the animation. The desired effect is that it appears at opacity 0 until the animation starts, 2 seconds after the page is loaded.
Is there a property I’m missing?
Typical!! Of course I search for ages before I post, then 2 minutes after posting I carry on looking and find the answer!
animation-fill-mode: backwards;was the solution.
I created this site http://www.css3builder.com it automates the css3 gallery creation by doing the math for the keyframes etc. etc. automatically. You can create one in less than 1 minute for all browsers.
Just a note that the upcoming IE10 will support keyframe syntax without prefixes. So, since IE9 doesn’t support keyframe animations, and since nobody will be using ‘IE10pp’ or whatever, then all examples should now omit the ‘-ms-’ part.
Also, all the examples on this page should be updated to include the standard syntax, for IE10, upcoming FF16 and other browsers that will eventually support keyframe animations unprefixed.
Page updated! Thanks Louis.
Android browser 2.2 supports it?
I have a animation in an infinite loop. Have any way to set an interval between the loops?
The property “animation-delay” just add a delay before the animation start, then loop without the delay.
I know that I can resolve with a simple “setInterval” script, but I’m trying to figure if have any way to reach to the expected result without that.
When an iOS device is in the process of completing animations #1 and a user scrolls down before animation #2 started, it will simply not load animations set to load later… Any workaround ?
Hey, it would be great if you include the
steps()timing function. You can see more details here, for example:https://hacks.mozilla.org/2011/09/taking-steps-with-css-animations/
Hello Chris for the basic animation you can use the simple code:
.box{
background: white;
-webkit-transition: background 5s;
}
.box:hover{
background: olive;
-webkit-transition: background 1s;
}
I don’t think anyone has asked this before:
how do you use the
@-webkit-keyframessyntax in SASS (or LESS, for that matter), where the@is a special character?