
Use a transition for a change between states and an animation for a defined sequence you can repeat. Transform changes where or how an element is drawn and can be used in either.
1. Animation shorthand
The animation shorthand combines name, duration, easing, delay, iteration count, direction, fill mode, and play state. Give the animation a nonzero duration and use a descriptive keyframe name.
.spinner { animation: spin 900ms linear infinite; }2. Animation subproperties
animation-iteration-count accepts a number or infinite. animation-direction controls whether playback reverses or alternates. animation-fill-mode controls presentation before or after playback. animation-play-state can be running or paused.
3. Define keyframes
Use from and to or percentages to mark points in the animation. The browser interpolates values between keyframes where the properties support it.
@keyframes spin { to { transform: rotate(1turn); } }
@keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-1rem); } }4. Compare the six original demos
Use the demos below to compare continuous rotation, sliding text, a bouncing ball, a spinning wheel, a loading indicator, and a heartbeat. Identify the property, easing, and repeat count in each. A loading animation should also have a textual status.
5. Respect reduced motion
Remove or simplify nonessential animation if the visitor asks for reduced motion. Preserve visible changes that communicate the application’s state.
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }