CSS Keyframe Animations: Syntax, Infinite Loops, and Performance
CSS Keyframe Animations: Syntax, Infinite Loops, and Performance
During boring lectures in school, have you ever drawn a stick figure cricket batsman in the bottom corner of your textbook?
- On page 1 (
0%): The batsman raises his bat. - On page 15 (
30%): The bowler releases the ball. - On page 30 (
60%): The bat strikes the ball with a loud crack! - On page 50 (
100%): The ball flies over the boundary ropes for a massive six!
When you hold the textbook edges and flip the pages rapidly with your thumb, the individual still drawings magically blend into a continuous, moving cartoon movie!
+-------------------------------------------------------------------------+
| THE FLIPBOOK OF CSS KEYFRAME ANIMATION |
| |
| Transition vs Animation: |
| * Transition: Needs a trigger (like hover) to morph from A to B. |
| * Animation: Starts automatically, visits 10 different waypoints, |
| and can loop infinitely forever! |
| |
| @keyframes swingBat { |
| 0% { transform: rotate(0deg); } |
| 50% { transform: rotate(-45deg); } <--- Intermediate Waypoints! |
| 100% { transform: rotate(90deg); } |
| } |
+-------------------------------------------------------------------------+While transitions are great for simple two-state hover shifts, CSS Animations with @keyframes give you director-level control over complex, multi-stage, continuous choreographies. In this tutorial, you will master keyframe syntax, infinite loops, and vital accessibility best practices.
1. Defining the Script: The @keyframes Rule
Before an element can animate, you must write the animation script using the @keyframes at-rule, followed by an animation name:
2. Attaching the Animation to an Element
Once your keyframe script is defined, attach it to your target HTML element using the animation properties:
The Professional Animation Shorthand
In production, we write all parameters in one clean declaration:
3. The Enigma of animation-fill-mode
Have you ever created an entrance animation where an element slides into view, but as soon as the animation ends, it snaps back to its original position or disappears?
That happens because of animation-fill-mode!
forwards!4. Web Accessibility: prefers-reduced-motion
Not all users enjoy spinning logos or bouncing cards. For people with vestibular motion disorders, spinning animations can cause dizziness, nausea, and headaches.
Modern operating systems (Windows, macOS, iOS, Android) have a setting called "Reduce Motion". You must always respect this setting in professional web design:
This single 8-line snippet ensures that users who requested reduced motion get instant, calm transitions without motion sickness!
5. Common Mistakes to Avoid (Do's and Don'ts)
| Bad Practice (Don't) | Good Practice (Do) | Why? |
|---|---|---|
Forgetting animation-fill-mode: forwards on entrance animations | Add forwards to entrance keyframes | Prevents elements from abruptly popping back to invisible or displaced states. |
Animating width, height, or margin in continuous loops | Strictly animate transform and opacity | Non-GPU properties trigger heavy continuous layout recalculations that drain phone batteries. |
| Making crucial content rely on an infinite animation to be readable | Use subtle ambient indicators (like live status pulses) | Constant vigorous movement distracts users from reading core content. |
Ignoring the prefers-reduced-motion accessibility standard | Provide static fallbacks for motion-sensitive users | Ensures WCAG 2.1 AA accessibility compliance for enterprise websites. |
6. Quick Revision Summary (Cheat Sheet)
@keyframes name { ... }: Defines the animation blueprint using percentages (0%,50%,100%).animation-iteration-count: infinite: Keeps the animation cycling forever without stopping (great for spinners and tickers).animation-direction: alternate: Bounces the animation back and forth (0% $\to$ 100% $\to$ 0%).animation-fill-mode: forwards: Locks the element into its final keyframe state after the animation finishes.prefers-reduced-motion: Accessible media query to mute or neutralize animations for users with motion sensitivity.
Multiple Choice Questions
1. Which CSS property value forces an element to stay frozen in its final keyframe state after the animation finishes?
A. animation-iteration-count: stop B. animation-fill-mode: forwards C. animation-play-state: frozen D. animation-direction: static Answer: B Explanation: animation-fill-mode: forwards ensures that the element retains the calculated style values from the last keyframe encountered during execution.
2. What is the fundamental difference between CSS Transitions and CSS Animations?
A. Transitions only work in Firefox, while animations work everywhere B. Transitions require a state trigger (like :hover) to interpolate between two states, while Animations run automatically across multiple intermediate waypoints C. Transitions can loop infinitely, but Animations cannot D. There is no difference; they are exact aliases Answer: B Explanation: Transitions interpolate between two states when triggered, whereas Animations can execute automatically, chain multi-step keyframe sequences, and loop continuously.
3. Which value of animation-iteration-count makes an animation loop continuously without ever stopping?
A. loop B. infinite C. continuous D. forever Answer: B Explanation: The keyword infinite instructs the browser to repeat the animation sequence endlessly.
4. Which media query allows developers to disable or tone down vigorous animations for users with vestibular balance disorders?
A. @media (screen: static) B. @media (prefers-reduced-motion: reduce) C. @media (accessibility: high) D. @media (no-animation) Answer: B Explanation: prefers-reduced-motion detects if the user has enabled the "Reduce Motion" accessibility preference in their operating system.
5. In a keyframe rule, which two properties can be animated continuously with 60fps GPU acceleration?
A. width and left B. transform and opacity C. border-width and margin D. font-size and padding Answer: B Explanation: transform and opacity are handled directly by the GPU compositing thread, avoiding expensive layout reflows and repaints.
7. Hands-on Practice Challenge: Live Admission Ticker & Radar Pulse
Build an animated School Admission Status component:
- 1A glowing green Live Radar Pulse badge that ripples outwards continuously using
transform: scale()andopacitywithanimation-iteration-count: infinite. - 2A continuous, rotating Loading Spinner for real-time seat availability updates.
- 3Full accessibility support using
@media (prefers-reduced-motion: reduce).
Practical Interactive Button and Card Effects: Micro-Interactions in Action
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.