Section 28: CSS Keyframe Animations
CSS animations use @keyframes to describe styles at specific points in an animation timeline. The animation property connects those keyframes to an element.
1. Define keyframes
@keyframes slide-in {
from { opacity: 0; transform: translateX(-20px); }
to { opacity: 1; transform: translateX(0); }
}You can also use percentages such as 0%, 50%, and 100% to define multiple stages.
2. Apply the animation
.notice {
animation-name: slide-in;
animation-duration: 500ms;
animation-timing-function: ease-out;
animation-delay: 100ms;
animation-iteration-count: 1;
animation-direction: normal;
animation-fill-mode: both;
}3. Useful animation properties
- animation-name: selects the keyframes.
- animation-duration: defines one cycle's duration.
- animation-iteration-count: sets repetitions; infinite repeats continuously.
- animation-direction: controls forward or alternate playback.
- animation-fill-mode: controls styles before or after playback.
- animation-play-state: pauses or runs the animation.
Runnable example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Keyframe Animation</title>
<style>
body { font-family: Arial, sans-serif; padding: 30px; }
.notice { max-width: 320px; padding: 20px; border-radius: 10px; background: #dcfce7; color: #166534; animation: appear 600ms ease-out both; }
.loader { width: 36px; height: 36px; margin-top: 24px; border: 4px solid #d1d5db; border-top-color: #2563eb; border-radius: 50%; animation: spin 800ms linear infinite; }
@keyframes appear {
from { opacity: 0; transform: translateY(12px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="notice">Your changes have been saved.</div>
<div class="loader" role="status" aria-label="Loading"></div>
</body>
</html>Common mistakes
- Forgetting to apply the animation name to an element.
- Using animation-fill-mode: forwards when the final state should not persist.
- Making important content appear only through animation, which can cause problems if motion is reduced or disabled.
- Animating many large elements continuously, which can waste battery and processing resources.
Practice exercise
Create a notification that slides in once and a circular loader that spins continuously. Use separate keyframe names for the two effects.
Knowledge check: 10 MCQs
- Which rule defines animation stages? Answer: @keyframes. Explanation: It describes styles at timeline positions.
- Which property chooses the animation? Answer: animation-name. Explanation: It refers to a named keyframes rule.
- What does infinite do for iteration count? Answer: Repeats indefinitely. Explanation: The animation continues until it is stopped or the element no longer participates.
- What does animation-duration control? Answer: The length of one cycle. Explanation: It determines the time for one iteration.
- What does alternate do? Answer: Alternates playback direction between iterations. Explanation: It plays forward and then backward on successive iterations.
- What does animation-play-state: paused do? Answer: Pauses the animation. Explanation: The current animation progress is held.
- What does animation-fill-mode: forwards do? Answer: Keeps the final keyframe's applicable styles after playback. Explanation: The animation's end styles persist.
- Can keyframes contain multiple percentages? Answer: Yes. Explanation: Intermediate stages can define a more complex sequence.
- Should every interface element animate continuously? Answer: No. Explanation: Excessive motion can distract and reduce performance.
- How can a circular loader be rotated continuously? Answer: Use a rotate() keyframe with infinite iteration. Explanation: The keyframes define rotation and the iteration count repeats it.