CSS Animations
Transitions animate between two states when something changes. Animations go further: they run on their own, can have many steps, can loop forever and can play in reverse. You describe the steps in a @keyframes rule and then attach it to an element with the animation property.
@keyframes
A keyframes rule has a name and a list of percentages (or from/to). Each step says what the styles look like at that point in the animation, and the browser fills in everything in between.
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.2); }
100% { transform: scale(1); }
}<!DOCTYPE html>
<html>
<head>
<style>
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.25); }
}
@keyframes slide {
from { transform: translateX(0); background: #42a5f5; }
to { transform: translateX(220px); background: #ec407a; }
}
body { font-family: system-ui, sans-serif; padding: 30px; }
.heart { font-size: 50px; display: inline-block; animation: pulse 1s ease-in-out infinite; }
.block { width: 60px; height: 60px; border-radius: 10px; margin-top: 20px; animation: slide 2s ease-in-out infinite alternate; }
</style>
</head>
<body>
<span class="heart">❤️</span>
<div class="block"></div>
</body>
</html>Animation properties
| Property | Purpose | Example values |
|---|---|---|
animation-name |
Which @keyframes to use |
pulse |
animation-duration |
Length of one cycle | 2s, 500ms |
animation-timing-function |
Speed curve per cycle | ease, linear, steps(4) |
animation-delay |
Wait before starting (negative starts partway through) | 0.5s, -1s |
animation-iteration-count |
How many times | 1, 3, infinite |
animation-direction |
Order of playback | normal, reverse, alternate, alternate-reverse |
animation-fill-mode |
Styles before or after running | none, forwards, backwards, both |
animation-play-state |
Pause or resume | running, paused |
The shorthand takes them in almost any order. The first time value is always the duration and the second is the delay:
animation: slide 2s ease-in-out 0.5s infinite alternate both;Direction and fill mode
alternate plays forward and then backward, which gives smooth back-and-forth motion. fill-mode: forwards keeps the final keyframe once the animation ends. Without it, the element snaps back to its original style.
<!DOCTYPE html>
<html>
<head>
<style>
@keyframes grow { from { width: 40px; background: #90a4ae; } to { width: 260px; background: #66bb6a; } }
body { font-family: system-ui, sans-serif; padding: 10px; }
.bar { height: 30px; margin: 8px 0; border-radius: 6px; color: white; font-size: 12px; padding-left: 6px; line-height: 30px; white-space: nowrap;
animation: grow 1.5s ease-out 3; }
.normal { animation-direction: normal; }
.alt { animation-direction: alternate; }
.fwd { animation-iteration-count: 1; animation-fill-mode: forwards; }
.none { animation-iteration-count: 1; }
</style>
</head>
<body>
<div class="bar normal">normal ×3</div>
<div class="bar alt">alternate ×3</div>
<div class="bar fwd">once + forwards (stays)</div>
<div class="bar none">once, no fill (snaps back)</div>
<p>Press Run again to replay.</p>
</body>
</html>Practical animations
Loaders, attention-grabbers and entrance effects are the most common uses. Several animations can run on one element, separated by commas, and a negative delay starts each copy at a different point in the cycle.
<!DOCTYPE html>
<html>
<head>
<style>
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes bounce { 0%, 80%, 100% { transform: translateY(0); } 40% { transform: translateY(-18px); } }
@keyframes fadeUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: none; } }
body { font-family: system-ui, sans-serif; display: flex; gap: 40px; align-items: center; padding: 40px 20px; }
.spinner { width: 46px; height: 46px; border: 6px solid #e0e0e0; border-top-color: #3f51b5; border-radius: 50%; animation: spin .9s linear infinite; }
.dots span { display: inline-block; width: 14px; height: 14px; margin: 0 3px; border-radius: 50%; background: #ff7043; animation: bounce 1.2s ease-in-out infinite; }
.dots span:nth-child(2) { animation-delay: -1s; }
.dots span:nth-child(3) { animation-delay: -.8s; }
.toast { padding: 12px 16px; background: #2e7d32; color: white; border-radius: 8px; animation: fadeUp .6s ease-out .3s both; }
</style>
</head>
<body>
<div class="spinner"></div>
<div class="dots"><span></span><span></span><span></span></div>
<div class="toast">Saved ✓</div>
</body>
</html>Tip:
fill-mode: bothcombined with a delay hides the element during the delay (thefromstate applies early), then keeps the final state. It's ideal for entrance animations.
Pausing and reduced motion
animation-play-state: paused freezes an animation, for example while the user hovers over it. Some people get dizzy or distracted by motion and turn on "reduce motion" in their operating system. Respect that setting with a media query.
<!DOCTYPE html>
<html>
<head>
<style>
@keyframes orbit { to { transform: rotate(360deg); } }
body { font-family: system-ui, sans-serif; display: grid; place-items: center; height: 240px; margin: 0; }
.sun { width: 160px; height: 160px; border-radius: 50%; border: 2px dashed #ffb300; position: relative; animation: orbit 4s linear infinite; }
.sun::before { content: "☀️"; position: absolute; inset: 0; display: grid; place-items: center; font-size: 40px; }
.planet { position: absolute; top: -14px; left: 66px; font-size: 26px; }
.sun:hover { animation-play-state: paused; }
@media (prefers-reduced-motion: reduce) {
.sun { animation: none; }
}
</style>
</head>
<body>
<div class="sun"><span class="planet">🌍</span></div>
<p>Hover over the orbit to pause it.</p>
</body>
</html>Transitions vs animations
| Transition | Animation | |
|---|---|---|
| Trigger | A property change (hover, class change) | Starts on its own when applied |
| Steps | Start and end only | Any number of keyframes |
| Looping | No | iteration-count: infinite |
| Best for | Hover and state changes | Loaders, attention, entrances |
Test yourself
Which rule defines the steps of a CSS animation?
- @animation
- @keyframes
- @steps
- @transition
An element snaps back to its original style when its one-time animation ends. What keeps the last keyframe?
- animation-direction: reverse
- animation-fill-mode: forwards
- animation-play-state: paused
- animation-iteration-count: 0
What does animation-direction: alternate do?
- Plays every other element
- Runs odd cycles forward and even cycles backward
- Reverses the timing function only
- Skips every second keyframe