CSS3 Animations
CSS animations let an element change its styles gradually through a sequence of steps you define, without any JavaScript. You use them for loading spinners, attention-grabbing pulses, slide-in banners and any motion that should run on its own, loop, or have more than a simple start and end state.
Defining the motion with @keyframes
An animation has two parts. First, a @keyframes rule describes what the element looks like at different points, using percentages or the words from (0%) and to (100%). Second, you attach that rule to an element with the animation properties.
@keyframes slide-in {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.toast {
animation: slide-in 0.4s ease-out both;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.spinner {
width: 32px;
height: 32px;
border: 4px solid #ccc;
border-top-color: #0d6efd;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
The spinner only defines the end state. When a keyframe is missing, the browser uses the element's normal styles for it.
The animation properties explained
| Property | What it controls |
|---|---|
animation-name | Which @keyframes rule to run. |
animation-duration | How long one cycle takes, such as 0.5s. |
animation-timing-function | The speed curve: ease, linear, ease-in-out or cubic-bezier(). |
animation-delay | Wait time before starting. |
animation-iteration-count | A number or infinite. |
animation-direction | normal, reverse or alternate for back-and-forth. |
animation-fill-mode | Whether first or last keyframe styles apply before or after running. |
The animation shorthand accepts these in almost any order. The one rule is that the first time value is the duration and the second is the delay.
Why the element jumps back at the end
A very common surprise: an element fades in, then snaps back to invisible when the animation finishes. That happens because, by default, keyframe styles apply only while the animation runs. Setting animation-fill-mode: forwards keeps the final keyframe, and both also applies the first keyframe during any delay so the element does not flash before starting.
Keeping animations smooth
Animating transform and opacity is cheap for the browser, since it can usually handle them on the compositor without recalculating layout. Animating width, height, top or margin forces layout work on every frame and can stutter on slower phones. To move something, prefer translate; to grow it, prefer scale.
Respecting users who prefer less motion
Some people feel dizzy or distracted by movement and turn on a "reduce motion" setting in their operating system. CSS exposes this through a media query, and it is good practice to tone down or switch off decorative animations there:
- Wrap large or looping effects in
@media (prefers-reduced-motion: no-preference) { ... }. - Or, inside
@media (prefers-reduced-motion: reduce), setanimation: noneon those elements. - Keep essential feedback, such as a short fade, but drop bouncing and parallax.
Animations versus transitions
Transitions react to a change in state, such as hover, and only go from A to B. Animations run on their own, can have many steps, and can repeat. If you just want a button colour to change smoothly on hover, a transition is simpler.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.