A notification that slides in, a loading spinner, a button that shakes when a form is invalid, a hero heading that fades up on page load: small animations help interfaces feel responsive and guide attention. CSS can handle nearly all of them without a line of JavaScript, and when done well they run smoothly even on modest phones.
The syntax has a few traps, though. Animations that snap back to the start when they finish, stutter on mobile, or play at the wrong moment are common. This guide explains how keyframe animations work, how each part of the animation shorthand behaves, and how to keep motion smooth and considerate.
Transitions versus animations
CSS has two motion systems, and picking the right one saves effort.
Transitions animate a property from its current value to a new one when something changes, such as a hover, a focus, or a class added by script. They have exactly two states.
.card {
transition: transform 0.2s ease-out, box-shadow 0.2s ease-out;
}
.card:hover {
transform: translateY(-4px);
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
}
Keyframe animations run a sequence you define with @keyframes, with as many steps as you like. They can start on their own when an element appears, loop, alternate direction and pause.
Use transitions for state changes the user causes. Use keyframe animations for anything that plays on its own, repeats, or has more than two stages.
Anatomy of a keyframe animation
@keyframes slideInUp {
from { transform: translateY(60px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
.toast {
animation: slideInUp 0.4s ease-out 0s 1 normal both;
}
from and to are aliases for 0% and 100%. You can add any percentages in between, and group several stops that share values:
@keyframes shake {
0%, 100% { transform: translateX(0); }
20%, 60% { transform: translateX(-8px); }
40%, 80% { transform: translateX(8px); }
}
The animation shorthand, value by value
The shorthand used by most generators lists values in this order:
animation: name duration timing-function delay iteration-count direction fill-mode;
- name: the
@keyframesrule to run. - duration: how long one cycle takes, such as
0.4sor400ms. Interface feedback usually sits between 150 and 500ms; longer feels sluggish. - timing-function: the easing curve (see below).
- delay: wait before starting. Negative values start part-way through the cycle.
- iteration-count: a number or
infinite. - direction:
normal,reverse,alternate(forwards then backwards) oralternate-reverse. - fill-mode: what styles apply before and after the animation.
Durations and delays are both times, and the first time value is always read as the duration, so keep them in this order.
fill-mode: why animations snap back
By default (none), keyframe styles only apply while the animation is running. When a fade-in finishes, the element jumps back to whatever its normal CSS says. If that is opacity: 0, it disappears again.
forwardskeeps the final keyframe's styles after the end.backwardsapplies the first keyframe during the delay, so an element with a delayed fade-in is not visible before it starts.bothdoes both, and is the safest choice for entrance animations.
Timing functions
The easing curve controls speed over time:
ease-outstarts fast and slows down: natural for things entering the screen.ease-instarts slow and speeds up: good for things leaving.ease-in-outis gentle at both ends: good for movement between two on-screen positions.linearis constant speed: right for spinners and progress bars.cubic-bezier(x1, y1, x2, y2)defines a custom curve. Y values outside 0-1 overshoot, creating a "back" or elastic feel, as incubic-bezier(0.34, 1.56, 0.64, 1).steps(n)jumps in n discrete steps, useful for sprite-sheet animation or a typewriter effect.
Create an animation with the generator
The CSS Animation Generator gives you presets, a live preview and the full CSS.
- Open the CSS Animation Generator and pick a preset: fadeIn, fadeOut, slideInLeft, slideInUp, slideInRight, bounce, pulse, shake, spin, zoomIn, zoomOut, flip, swing, heartbeat or wobble.
- Set Duration (0.1-10s) and Delay (0-5s) with the sliders.
- Choose a Timing function, including two overshoot curves and
steps(8, end), plus Direction and Fill mode. - Leave Iterations on infinite or switch it off and enter a count.
- Edit the Keyframes (editable) box to change any step, and rename the animation in the name field beside it.
- Click Replay to watch the preview again, then copy the CSS. It includes the
@keyframesblock, an.animatedclass with the shorthand, and aprefers-reduced-motionrule that turns the animation off for users who ask for less motion.
Keeping animations smooth
Browsers render a frame in several stages: calculating layout, painting pixels and compositing layers. Animating properties that change layout (width, height, top, left, margin) forces the first two stages on every frame, which can drop frames on slower devices.
transform and opacity can usually be handled by the compositor alone, which is why nearly every preset uses them:
- move with
transform: translate(), notleftortop; - resize with
transform: scale(), notwidth; - fade with
opacity, notvisibilitytoggling.
will-change: transform can hint the browser to prepare a layer, but add it only where you have measured a problem; overusing it wastes memory. Animating box-shadow or filter blur is comparatively expensive; animating a pseudo-element's opacity to reveal a shadow is a common workaround.
Respecting reduced motion
Some people experience dizziness or nausea from motion on screen, and operating systems offer a "reduce motion" setting. CSS can detect it:
@media (prefers-reduced-motion: reduce) {
.animated {
animation: none;
}
}
Rather than removing everything, you can also replace big movements with a simple fade. WCAG also asks that anything moving automatically for more than five seconds, alongside other content, can be paused, stopped or hidden.
Pitfalls
The animation does not play again
A keyframe animation runs when the rule first applies. To replay it, remove the class, force a reflow, and add it back, or change the animation name.
Transforms overwrite each other
If an element already has transform: rotate(5deg) and the keyframes set transform: translateY(0), the rotation is lost during the animation. Include the rotation in every keyframe, or use the individual translate, rotate and scale properties, which combine instead of replacing.
Hover animations flicker
If a hover animation moves the element out from under the cursor, the hover ends and the animation restarts. Animate a child element, or apply the hover to a stable parent.
Related effects
Motion pairs well with other visual effects. The text shadow generator creates glows that can pulse, the CSS button generator adds hover transitions to buttons, and the clip-path generator shapes can be animated for reveal effects; the clip-path guide explains the rules for morphing them.
Frequently asked questions
What is the difference between a CSS transition and an animation?
A transition animates between two states when a property changes. A keyframe animation runs a defined sequence of any number of steps, can start on its own, and can loop.
Why does my CSS animation reset at the end?
The default fill mode is none, so final styles are dropped when the animation ends. Use animation-fill-mode: forwards or both.
Which properties are cheapest to animate?
transform and opacity. They can usually be animated without recalculating layout or repainting, which keeps motion smooth.
How do I make an animation loop forever?
Set the iteration count to infinite. Adding alternate as the direction makes it play back and forth smoothly instead of jumping to the start.
Should I disable animations for reduced motion?
Yes, or replace them with subtler versions. Wrap the change in @media (prefers-reduced-motion: reduce).