Full answer
A transition animates between two states when something triggers a change, like hover. A keyframe animation is defined by a @keyframes block that lists steps, and it can start on its own, repeat, run back and forth, and pass through many steps.
An animation is controlled with these properties, usually in the animation shorthand:
animation-name: the@keyframesto use.animation-duration,animation-timing-function,animation-delay.animation-iteration-count: a number orinfinite.animation-direction:normal,reverse,alternate.animation-fill-mode: whether the first or last frame styles apply before and after the run.forwardskeeps the last frame, which is the usual fix for "the element jumps back at the end".animation-play-state:runningorpaused.
Some users get headaches or dizziness from motion. Operating systems have a "reduce motion" setting, and CSS can read it with prefers-reduced-motion. Respect it by removing or shortening large movement, spinning and parallax.
The first box pulses forever. The second slides 100px and, because of forwards, stays there. Without forwards it would snap back to its start. With reduced motion switched on, the first box does not animate and the second jumps to the end almost instantly.
Animate transform and opacity where possible, since they avoid layout work. Never rely on animation alone to convey meaning, and give users a pause control for anything that moves for more than five seconds.
<style>
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.15); } }
@keyframes slide { from { transform: translateX(0); } to { transform: translateX(100px); } }
.pulse { width: 40px; height: 40px; background: tomato; animation: pulse 1.2s ease-in-out infinite; }
.once { width: 40px; height: 40px; background: teal; animation: slide 0.1s ease-out forwards; }
@media (prefers-reduced-motion: reduce) {
.pulse { animation: none; }
.once { animation-duration: 0.001s; }
}
</style>
<div class="pulse" id="pulse"></div>
<div class="once" id="once"></div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Transitions need a trigger and two states; keyframes can run on their own.
- Key properties: name, duration, timing function, delay, iteration count, direction, fill mode.
- forwards keeps the end state after the animation finishes.
- prefers-reduced-motion lets CSS respect the user's system setting.
- Animate transform and opacity where possible.
Common mistakes
- an infinite animation on a large area that cannot be stopped. It distracts users and can make some feel unwell.
- forgetting animation-fill-mode: forwards and finding that the element resets to its first state after the animation ends.
Follow-ups
- can you pause a CSS animation from JavaScript?
- How do you pause a CSS animation from JavaScript?
- What does animation-direction: alternate do?
Tips
- Test reduced motion by turning it on in your operating system, or use the Rendering panel in DevTools to emulate it.
- Mention reduced motion without being asked; it shows care for users.
- Name one rule: pause control for anything moving more than five seconds.