Transitions animate a change between two states. Animations go further — they let you script motion through many steps and loop it forever, all without JavaScript. Think loading spinners, a gently pulsing button, a fading-in section. Used with restraint, they add real life to a page.
1@keyframes and the animation property
You build an animation in two parts. First, define the steps with @keyframes, giving the name and what changes at each point (from/to, or percentages):
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.15); }
100% { transform: scale(1); }
}Then apply it with the animation shorthand — name, duration, timing, and how many times (or infinite):
.heart {
animation: pulse 1.2s ease-in-out infinite;
}
- Define motion steps with
@keyframes name { ... }usingfrom/toor percentages. - Run it with
animation: name duration timing iteration;(useinfiniteto loop). - Animations can run many steps and loop — unlike a transition's single state change.
2Control, restraint and accessibility
A few more controls you'll use:
animation-delay— wait before starting.animation-direction: alternate— play forwards then backwards (great for pulses).animation-fill-mode: forwards— keep the final frame after it ends.animation-play-state: paused— pause it (e.g. on hover).
Respect reduced motion
Some users get dizzy or nauseous from motion. Always provide a calmer experience for them:
@media (prefers-reduced-motion: reduce) {
* { animation: none !important; }
}- Control with
animation-delay,-direction: alternate,-fill-mode: forwards,-play-state: paused. - Animate with restraint — to guide attention, not decorate everything.
- Honour
@media (prefers-reduced-motion: reduce)for users sensitive to motion.
★ Practical: a loading spinner and a nudge
In any playground:
- Define a
spinkeyframe and make a circular spinner loopinfiniteandlinear. - Define a
pulsekeyframe and apply it to a call-to-action button. - Add
animation-direction: alternateto one and see the difference. - Add a
prefers-reduced-motionmedia query that disables the animations.
Ready to test yourself?
Take the short quiz. Score 60% or more to mark this lesson complete.
Start the quiz →💡 Log in to save your progress and earn the certificate.