Basic syntax for keyframe animations
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
-
The keyframe can go
from/to, but it can also be more specific with percentages to define the steps:0%/25%/50%/100%… You can also only specify atoor afromvalue. In this case it will start from the element’s current value, or end to the element’s current value. -
Each keyframe animation needs a name, then it can be used anywhere in the css code, as a value of the
animationcss property, like this:
.element {
animation: fadeIn 1000ms;
}
Take full benefit from the animation property
animation needs:
- the name of the keyframe animation to apply
- the duration of the animation: the
fromblock is applied immediately, then the element transitions smoothly to thetoblock content during that duration.
Optionally, the animation property can be assigned other keywords (in any order):
infinite(useful for loading spinners!), or a number to specify how many times we want to repeat the animationlinear/ease-in-out… a timing functionalternate/reverse: the animation “direction”:reversewill play the animation from 100% to 0%,alternatewill alternate (start at 0% the first time, start at 100% the next time)- a delay. As it requires a time value, it must be specified after the duration.
- a “fill mode”:
forwardswill persist the final css of the keyframe animation forwards in time.backwardswill apply thefromcss properties to the element before the animation starts (eg. if we apply a delay). If we want to persist both the starting css and the ending css, we can useboth
animation is a shorthand property. We could also write:
.element {
animation-name: fadeIn;
animation-duration: 1000ms;
animation-iteration-count: infinite;
animation-timing-function: ease-in-out;
animation-direction: alternate;
/* not very coherent with the previous styles applied */
animation-fill-mode: forwards;
}
Keyframes are full of possibilities!
-
Several keyframes can be applied to the element (but it’s usually not recommended because keyframes can overwrite each other).
-
The keyframe can be defined anywhere in the css code, doesn’t have to be defined before it’s used (it’s hoisted).