Percentages within a @keyframes block, from 0%, or the from keyword, to 100%, or to, mark specific points along the animation's overall timeline, each specifying what the animated properties' values should be at that exact point, with the browser automatically interpolating smooth intermediate values between each defined keyframe. Unlike a transition, which only ever animates between exactly two states, a starting value and an ending value, @keyframes can define any number of intermediate steps in between, like 0%, 50%, and 100%, enabling considerably more complex, multi-stage animations, like a bouncing or pulsing effect, that a simple two-state transition alone couldn't represent.
1Understanding Keyframes
Percentages within a @keyframes block, from 0%, or the from keyword, to 100%, or to, mark specific points along the animation's overall timeline, each specifying what the animated properties' values should be at that exact point, with the browser automatically interpolating smooth intermediate values between each defined keyframe. Unlike a transition, which only ever animates between exactly two states, a starting value and an ending value, @keyframes can define any number of intermediate steps in between, like 0%, 50%, and 100%, enabling considerably more complex, multi-stage animations, like a bouncing or pulsing effect, that a simple two-state transition alone couldn't represent.
@keyframes only defines the animation's sequence of styles — it does nothing on its own until an element's animation property actually references that keyframe name, along with a duration, to actually play it.
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.element {
animation: fadeIn 0.5s ease;
}2Practical Example
Here is a real-world application of Keyframes showing how it is used in production CSS code.
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-20px); }
}
.ball {
animation: bounce 1s ease-in-out infinite;
}3Best Practices
Follow these guidelines when working with Keyframes:
1. Use @keyframes, rather than a transition, whenever an animation needs more than just a simple two-state, start-to-end change, like a multi-stage bounce or pulse effect
2. Reference the exact @keyframes name in an element's animation property, along with an explicit duration, since defining @keyframes alone has no visible effect without that
3. Use percentage-based keyframe steps, like 0%, 50%, 100%, for finer control over an animation's intermediate stages, rather than being limited to just a start and end point
Tip: @keyframes only defines the animation's sequence of styles — it does nothing on its own until an element's animation property actually references that keyframe name, along with a duration, to actually play it.
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.element {
animation: fadeIn 0.5s ease;
}