Unlike a transition, which requires an actual property change to trigger it, an animation referencing a valid @keyframes name begins playing automatically as soon as the element renders, without needing any specific triggering event at all, and can be set to loop a specific number of times, or indefinitely with animation-iteration-count: infinite. animation-fill-mode, a related property not included in the basic shorthand list above, controls whether the element retains the animation's final keyframe styles after it finishes playing, or reverts back to its original, pre-animation styles, which matters for animations that shouldn't simply snap back to their starting appearance once complete.
1Understanding Animation
Unlike a transition, which requires an actual property change to trigger it, an animation referencing a valid @keyframes name begins playing automatically as soon as the element renders, without needing any specific triggering event at all, and can be set to loop a specific number of times, or indefinitely with animation-iteration-count: infinite. animation-fill-mode, a related property not included in the basic shorthand list above, controls whether the element retains the animation's final keyframe styles after it finishes playing, or reverts back to its original, pre-animation styles, which matters for animations that shouldn't simply snap back to their starting appearance once complete.
Set animation-fill-mode: forwards when an animation's final keyframe styles should persist after it finishes playing — without it, the element reverts back to its original, pre-animation styles the instant the animation completes, which often isn't the intended, desired behavior.
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.hero {
animation: fadeIn 1s ease forwards;
}2Practical Example
Here is a real-world application of Animation showing how it is used in production CSS code.
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.spinner {
animation: spin 1s linear infinite;
}3Best Practices
Follow these guidelines when working with Animation:
1. Use animation, rather than a transition, for effects that should play automatically on render, or that need more than two keyframe states, or that need to loop repeatedly
2. Set animation-fill-mode: forwards when an animation's ending appearance should persist rather than snapping back to the original pre-animation styles once it finishes
3. Use animation-iteration-count: infinite specifically for continuously repeating effects, like a loading spinner, and a specific finite number for effects that should play a set number of times and then stop
Tip: Set animation-fill-mode: forwards when an animation's final keyframe styles should persist after it finishes playing — without it, the element reverts back to its original, pre-animation styles the instant the animation completes, which often isn't the intended, desired behavior.
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.hero {
animation: fadeIn 1s ease forwards;
}