The main idea with a CSS keyframe animation is that it'll interpolate between different chunks of CSS.
For example, here we define a keyframe animation that will smoothly ramp an element's horizontal position from -100%
to 0%
:
@keyframes slide-in {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0%);
}
}
Each @keyframes
statement needs a name! In this case, we've chosen to name it slide-in
. You can think of this like a global variable.*
Keyframe animations are meant to be general and reusable. We can apply them to specific selectors with the animation
property:
<style>
/* Create the animation... */
@keyframes slide-in {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0%);
}
}
/* ...and then apply it: */
.box {
animation: slide-in 1000ms;
}
</style>
<div class="box">
Hello World
</div>
(To re-run the animation, refresh the βResultβ pane by clicking the icon.)
As with the transition
property, animation
requires a duration. Here we've said that the animation should last 1 second (1000ms).
The browser will interpolate the declarations within our from
and to
blocks, over the duration specified. This happens immediately, as soon as the property is set.
We can animate multiple properties in the same animation declaration. Here's a fancier example that changes multiple properties:
<style>
@keyframes drop-in {
from {
transform:
rotate(-30deg) translateY(-100%);
opacity: 0;
}
to {
transform:
rotate(0deg) translateY(0%);
opacity: 1;
}
}
.box {
animation: drop-in 1000ms;
}
</style>
<div class="box">
Hello World
</div>
Top comments (0)