CSS Animations and Transitions: Smooth, Fast and Accessible Motion
What are CSS animations?
CSS animations change an element's styles over time without JavaScript. A transition animates between two states when a property changes, for example on hover. A @keyframes animation runs a defined sequence of steps that can start on its own, repeat and alternate. Animating transform and opacity keeps motion smooth.
Transitions versus @keyframes animations
| Transition | @keyframes animation | |
|---|---|---|
| Starts when | A property value changes (hover, focus, class added) | The animation is applied, or on load |
| Steps | Two states: from and to | Any number of keyframes |
| Repeats | No | Yes, with animation-iteration-count |
| Typical use | Hover effects, menus opening, colour changes | Loading spinners, attention cues, entrance effects |
Reach for a transition first. Most interface motion is a response to a state change, and a transition handles it with one line. Use keyframes when the motion needs more than two states or must run without a trigger.
How transitions work
A transition is declared on the element in its normal state. Whenever a listed property changes, the browser animates from the old value to the new one.
.button {
background: #0b57d0;
transform: translateY(0);
transition: background-color 200ms ease, transform 200ms ease-out;
}
.button:hover,
.button:focus-visible {
background: #0842a0;
transform: translateY(-2px);
}| Property | Controls |
|---|---|
transition-property | Which properties animate; list them rather than using all |
transition-duration | How long it takes, in s or ms |
transition-timing-function | The speed curve: ease, linear, ease-in-out, cubic-bezier(), steps() |
transition-delay | Wait before starting |
Listing properties explicitly avoids surprises: with transition: all, any later change, such as a width change at a breakpoint, animates too. Apply the same effect to :focus-visible as to :hover so keyboard users get the same feedback.
Building @keyframes animations
Define the steps once with @keyframes, then attach them to any element with the animation properties.
@keyframes spin {
to { transform: rotate(360deg); }
}
@keyframes fade-up {
from { opacity: 0; transform: translateY(1rem); }
to { opacity: 1; transform: translateY(0); }
}
.spinner {
width: 2rem; height: 2rem;
border: 3px solid #ccc; border-top-color: #0b57d0; border-radius: 50%;
animation: spin 800ms linear infinite;
}
.card {
animation: fade-up 400ms ease-out both;
}
.card:nth-child(2) { animation-delay: 100ms; }
.card:nth-child(3) { animation-delay: 200ms; }| Property | Controls |
|---|---|
animation-name | Which @keyframes to run |
animation-duration | Length of one cycle |
animation-iteration-count | A number or infinite |
animation-direction | normal, reverse, alternate |
animation-fill-mode | Whether the first or last keyframe styles apply before or after running; both covers both |
animation-play-state | running or paused |
Without animation-fill-mode: both, the delayed cards above would show at full opacity, then jump to invisible when their animation starts. To start or restart an animation from JavaScript, add or remove a class, as shown in the JavaScript events guide.
Performance: why transform and opacity are fast
Each frame, the browser may need to do three kinds of work: layout (working out sizes and positions), paint (drawing pixels) and composite (combining layers). Changing width, height, top, left or margin triggers layout for the element and often its neighbours, on every frame. Changing transform or opacity can usually be handled at the compositing stage, often on the GPU, so the animation stays smooth even when the main thread is busy.
| Instead of animating | Animate |
|---|---|
left / top | transform: translate() |
width / height | transform: scale() |
visibility flicker or display | opacity |
box-shadow size on hover | opacity of a pseudo-element that already has the larger shadow |
will-change: transform hints that an element is about to animate so the browser can prepare a layer. Use it sparingly on the few elements that need it, because each extra layer uses memory. Watch the Performance panel in dev tools for long frames and layout work during an animation. The web performance guide explains how this links to responsiveness metrics.
Accessible motion and reduced motion settings
Motion can cause dizziness, nausea or headaches for people with vestibular disorders, and constant movement distracts many readers. Operating systems offer a "reduce motion" setting, and CSS exposes it through the prefers-reduced-motion media feature.
/* Motion only for people who have not asked to reduce it */
@media (prefers-reduced-motion: no-preference) {
.card { animation: fade-up 400ms ease-out both; }
html { scroll-behavior: smooth; }
}
/* Or tone existing motion down */
@media (prefers-reduced-motion: reduce) {
.spinner { animation-duration: 2s; } /* slower, but still shows progress */
.hero-slide { animation: none; }
}- Reduced motion does not mean no feedback: keep colour and opacity changes, and remove large movement, zooming and parallax.
- WCAG requires a way to pause, stop or hide anything that moves automatically for more than five seconds alongside other content.
- Never make content flash more than three times in any one second.
- Keep interface transitions short, roughly 150 to 300 milliseconds, so the interface feels responsive rather than slow.
The media queries guide covers this and other preference features in more detail.
Adding an animation step by step
- Decide what the motion communicates, such as feedback, a state change or progress; skip motion that has no purpose.
- Use a transition if there are two states, or
@keyframesif there are more or it must repeat. - Animate
transformandopacitywherever possible. - Choose a short duration and an easing curve;
ease-outsuits elements entering the screen. - Wrap large or decorative motion in a
prefers-reduced-motionquery. - Test with reduced motion emulated in dev tools and check for dropped frames in the Performance panel.
Practical exercise: animate a notification toast
Build a small "Saved" notification that slides in from the bottom of the screen, using the styling basics from our CSS guide.
- Position the toast at the bottom of the viewport and hide it with
opacity: 0andtransform: translateY(150px). - Add a
.is-visibleclass that setsopacity: 1andtransform: none, with a 250ms transition on both properties. - Add a button that toggles the class with JavaScript, and give the toast
role="status"so screen readers announce it. - Create a
@keyframespulse for an icon inside the toast that runs twice and stops. - Under
prefers-reduced-motion: reduce, keep the fade but remove the slide and the pulse.
You are done when the toast moves smoothly with no layout work in the Performance panel, fades without sliding when reduced motion is emulated, and is announced by a screen reader. Adding polish like this to real project pages is part of SkillNewton's PHP foundation course.
Common mistakes
- Animating
width,height,toporleft, which forces layout on every frame and causes jank. - Using
transition: all, so unrelated property changes animate unexpectedly. - Ignoring
prefers-reduced-motionfor large, looping or parallax effects. - Adding
will-changeto many elements, which wastes memory instead of helping. - Making interface transitions so long that the site feels sluggish to use.
- Forgetting
animation-fill-mode, so elements jump before a delayed animation starts or after it ends.
Frequently asked questions
Can CSS transitions animate display: none?
Traditionally no, because display is not interpolated. Current browsers support transition-behavior: allow-discrete together with @starting-style for this, but fading opacity and toggling visibility remains a simple, well-supported approach.
Should I use CSS or JavaScript for animation?
Use CSS for state changes and simple sequences. Use JavaScript, such as the Web Animations API with element.animate(), when motion depends on user input, must be paused or reversed precisely, or needs values calculated at runtime.
What does the steps() timing function do?
It splits the animation into a fixed number of jumps instead of a smooth curve, which suits sprite-sheet animations or a typewriter effect where characters appear one at a time.
Do animations affect accessibility audits?
Yes. Auto-playing motion without a pause control, or flashing content, can fail WCAG success criteria. Our accessibility guide explains how to audit a page.