What are CSS Animations?
CSS animations allow you to create complex, multi-step animations using @keyframes. Unlike transitions which require triggers (like hover), animations can run automatically, loop infinitely, and have fine-grained control over each step of the animation sequence.
Basic Syntax
Keyframes with Percentages
Animation Properties
| Property | Description | Example |
|---|---|---|
| animation-name | Name of the @keyframes rule | bounce, fadeIn |
| animation-duration | Length of one cycle | 0.5s, 2000ms |
| animation-timing-function | Speed curve | ease, linear, cubic-bezier() |
| animation-delay | Delay before start | 0.2s, 1s |
| animation-iteration-count | Number of cycles | 1, 3, infinite |
| animation-direction | Play direction | normal, reverse, alternate |
| animation-fill-mode | Styles before/after | none, forwards, backwards |
| animation-play-state | Pause or play | running, paused |
Shorthand Syntax
Animation Directions
Fill Modes Explained
| Value | Before Animation | After Animation |
|---|---|---|
| none | No styles | Reverts to original |
| forwards | No styles | Retains last keyframe |
| backwards | First keyframe | Reverts to original |
| both | First keyframe | Retains last keyframe |
Practical Examples
Controlling Animation with JavaScript
Animation Events
Performance Best Practices
- Animate transforms and opacity – These use compositor thread (GPU accelerated)
- Avoid animating layout properties – width, height, margin, padding trigger reflows
- Use will-change property – Hints browser to optimize:
will-change: transform; - Keep animations under 60fps – Test with Chrome DevTools → Performance tab
- Respect prefers-reduced-motion – Provide accessible alternatives
Accessibility: Reduced Motion
Animation vs Transition Comparison
| Feature | Transitions | Animations |
|---|---|---|
| Trigger Required | Yes (hover, focus, class change) | No (can auto-start) |
| Keyframes | Only start/end (from/to) | Multiple steps (0% to 100%) |
| Looping | No (single cycle) | Yes (infinite) |
| Control | Simple | Fine-grained |
| Best For | Interactive feedback | Decorative/loading animations |
Common Mistakes
- Animating too many properties – Hurts performance
- Too long durations – Frustrating for users (loading spinners excepted)
- Forgetting fill-mode – Animations jump back to start without
forwards - Not testing on real devices – Desktop smooth ≠ mobile smooth
- Ignoring accessibility – Motion sensitivity is real
Conclusion
CSS animations bring websites to life with smooth, performant motion. They're perfect for loading indicators, entrance animations, attention-grabbing effects, and interactive storytelling. Combine with transforms and opacity for the best performance, and always respect reduced motion preferences.