Sub topic 7
Describing multi-step motion with @keyframes
This chapter assumes you can write a basic transition, you know what transform and opacity do, and you are comfortable with CSS selectors and class names.
Why keyframes exist
A transition moves between two states, and a state change such as :hover must trigger it. Sometimes you need more than that. You may want an element to move through three or more steps, or to start on page load with no trigger at all. For those cases you describe the steps in an @keyframes rule and attach it to an element with the animation properties.
The shape of an @keyframes rule
An @keyframes rule has a name you choose and a list of keyframes. Each keyframe is a percentage of the animation's duration, followed by the declarations that apply at that moment.
@keyframes pulse {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(1.1);
}
}
.badge {
animation: pulse 1.5s ease-in-out infinite;
}The browser calculates every frame between the keyframes. Here the badge is at normal size at 0%, 10% larger at 50%, and back to normal at 100%. The comma list 0%, 100% shares one block between two keyframes, so the start and end values cannot drift apart.
from is another name for 0%, and to is another name for 100%. Use whichever reads better. Percentages are better once you have more than two steps.
Leaving out the start or end
If you leave out the 0% or 100% keyframe, the browser fills it in with the element's normal computed value for each property you animate. This is useful for entry effects:
@keyframes slide-in {
from {
opacity: 0;
transform: translateX(-24px);
}
}
.panel {
animation: slide-in 300ms ease-out both;
}No to block is needed. The panel ends at its ordinary opacity and position.
Easing applies per segment
The timing function in the animation shorthand is applied between each pair of keyframes, not across the whole animation. In pulse, the ease-in-out curve runs once from 0% to 50% and again from 50% to 100%. That is why the pulse feels smooth at the peak instead of speeding through it.
You can override the curve for one segment by putting animation-timing-function inside a keyframe. It controls the segment that starts at that keyframe.
@keyframes bounce-drop {
0% {
transform: translateY(-100px);
animation-timing-function: ease-in;
}
60% {
transform: translateY(0);
animation-timing-function: ease-out;
}
80% {
transform: translateY(-20px);
animation-timing-function: ease-in;
}
100% {
transform: translateY(0);
}
}
.ball {
animation: bounce-drop 1s linear;
}Rules that cause surprises
- Keyframe order in the source does not matter. The browser sorts by percentage.
- If two keyframes share the same percentage, the later one wins for the properties it sets.
!importantinside a keyframe is invalid, so that declaration is ignored.- Percentages outside 0% to 100% invalidate that keyframe.
- If two
@keyframesrules share a name, the last one in the stylesheet is used.
Keep property lists consistent
Each animated property should appear in a compatible form in the keyframes that set it. For transform, use the same functions in the same order, for example translateY() then scale() in every keyframe. If the lists do not match, the browser may fall back to interpolating the matrix, which can produce unexpected paths.
Exercise
Write an @keyframes rule named shake that moves an element 8px left at 25%, 8px right at 75%, and leaves it at its normal position at the start and end. Then apply it to .error for 400ms.
Show answer
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-8px); } 75% { transform: translateX(8px); } } .error { animation: shake 400ms ease-in-out; }