Skip to content

Courses / CSS3 Mastery: Beginner to Advanced / CSS Keyframe Animations

2,087 people viewed this

CSS Keyframe Animations

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

  1. Which rule defines animation stages? Answer: @keyframes. Explanation: It describes styles at timeline positions.
  2. Which property chooses the animation? Answer: animation-name. Explanation: It refers to a named keyframes rule.
  3. What does infinite do for iteration count? Answer: Repeats indefinitely. Explanation: The animation continues until it is stopped or the element no longer participates.
  4. What does animation-duration control? Answer: The length of one cycle. Explanation: It determines the time for one iteration.
  5. What does alternate do? Answer: Alternates playback direction between iterations. Explanation: It plays forward and then backward on successive iterations.
  6. What does animation-play-state: paused do? Answer: Pauses the animation. Explanation: The current animation progress is held.
  7. What does animation-fill-mode: forwards do? Answer: Keeps the final keyframe's applicable styles after playback. Explanation: The animation's end styles persist.
  8. Can keyframes contain multiple percentages? Answer: Yes. Explanation: Intermediate stages can define a more complex sequence.
  9. Should every interface element animate continuously? Answer: No. Explanation: Excessive motion can distract and reduce performance.
  10. 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.

Rate this course

No ratings yet — yours can be the first.

Stars show on this course. Your written note stays private.

Your stars