CSS Animations and Motion Performance
Motion is the fastest way to communicate change: a drawer sliding in says where it went, a subtle lift on hover says this button is live, a skeleton shimmer says content is still arriving. CSS gives you two tools for all of it â transitions for state changes and at-rules for multi-step sequences â and, when used on compositor-friendly properties, the browser can run them off the main thread so scrolling stays smooth. This lesson builds both tools from the ground up, then covers timing, staggering, scroll-driven animation and the accessibility rules every motion-enabled interface needs.
Transitions versus animations: pick the right tool
A transition interpolates a property between two states you define in ordinary rules: base state in the default rule, target state in a state rule such as :hover or an added class. When the computed value changes, the browser animates across the duration. Transitions are ideal for hover feedback, toggles and anything driven by a single before-and-after pair.
An animation runs a @keyframes script independently of state changes: you name the sequence, give it a duration, iteration count and direction, and it plays on schedule. Animations fit entrances, loops, staggered list reveals and anything needing more than two keyframes or a defined repeat.
| Question | Transition | Animation |
|---|---|---|
| How many defined states? | Two: from and to | Any number of percentage stops |
| What starts it? | A computed-value change you already caused | A class, attribute or direct call |
| Repeat and direction? | Not natively | infinite, alternate, reverse supported |
| Best for | Hover, focus, toggles | Entrances, spinners, marquees, staggers |
| Removed on style revert? | Yes, it animates back | Keeps final fill state if you set forwards |
A practical rule: if the interface already has a class toggle, drive a transition off that class. Reach for @keyframes when you need choreography the state machine cannot express.
Building keyframe sequences
Keyframes accept from and to or explicit percentages, and any animatable property can participate. Use the fill mode deliberately â without forwards, the element snaps back to its pre-animation style the moment the sequence ends, which is correct for transient effects and wrong for entrances that should hold.
@keyframes card-enter {
from {
opacity: 0;
transform: translateY(16px) scale(0.98);
}
to {
opacity: 1;
transform: translateY(0) scale(1);
}
}
@keyframes pulse-soft {
0%, 100% { opacity: 1; }
50% { opacity: 0.55; }
}
@keyframes shimmer {
from { background-position: -200% 0; }
to { background-position: 200% 0; }
}
.notification-list {
display: grid;
gap: 0.75rem;
}
.notification {
border: 1px solid #e2e8f0;
border-radius: 10px;
padding: 1rem;
opacity: 0;
animation: card-enter 420ms cubic-bezier(0.22, 1, 0.36, 1) forwards;
}
.notification:nth-child(1) { animation-delay: 0ms; }
.notification:nth-child(2) { animation-delay: 80ms; }
.notification:nth-child(3) { animation-delay: 160ms; }
.notification:nth-child(4) { animation-delay: 240ms; }
.notification[data-pending="true"] .timestamp {
animation: pulse-soft 1.6s ease-in-out infinite;
}
.skeleton {
border-radius: 8px;
background: linear-gradient(90deg, #e2e8f0 25%, #f8fafc 50%, #e2e8f0 75%);
background-size: 200% 100%;
animation: shimmer 1.4s linear infinite;
}
.spinner {
inline-size: 2.5rem;
block-size: 2.5rem;
border: 3px solid #e2e8f0;
border-block-start-color: #2563eb;
border-radius: 50%;
animation: spin 800ms linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
.notification {
opacity: 1;
animation: none;
}
.skeleton, .spinner, .notification[data-pending="true"] .timestamp {
animation: none;
}
}
The stagger uses animation-delay on each child, cascading a four-item list in half a second without any JavaScript timing. The reduced-motion block is not optional decoration â it is part of the feature, shipping in the same rule set as the animation itself.
Easing and timing that feel natural
Duration sets the budget; easing sets the personality. Linear easing feels mechanical and is right only for constant-rate effects such as spinners. Ease-out â fast at first, settling at the end â suits entrances and anything that should feel responsive to input. Ease-in fits exits. The CSS ease-in-out family covers most cases, while cubic-bezier() lets you author custom curves: a curve like cubic-bezier(0.22, 1, 0.36, 1) gives a confident overshoot-free settle that designers reach for constantly.
Match duration to the size of the change. Hover feedback lands in the 120â200 millisecond range; panels and drawers run 250â400; large page-level entrances rarely need more than 500. Longer durations make interfaces feel sluggish, and users perceive any interaction above roughly 300 milliseconds as waiting rather than reacting. When an animation communicates failure or urgency, shorten it; when it accompanies a deliberate user action, it can be a touch slower.
Animating compositor-friendly properties
The browser renders in stages: style, layout, paint, then composite. Animating layout-triggering properties â width, height, top, left, margin â forces recalculation and repaint every frame, which is exactly what causes jank during scroll. Transform and opacity are handled by the compositor, so they keep the main thread free. A practical split:
| Property group | Example | Cost per frame | Use for motion? |
|---|---|---|---|
| Transform | translate, rotate, scale | Compositor only | Yes â default choice |
| Opacity | opacity fades | Compositor only | Yes |
| Filter | blur, brightness | Paint on some engines | Short, small-area effects |
| Layout properties | width, height, margin, top | Full style and layout pass | Avoid; animate transform instead |
| Paint properties | color, background-color | Repaint | Small elements are fine |
The classic fix is structural: never animate top: 0 to top: 200px; animate transform: translateY(200px) to translateY(0) on an element whose layout position never changes. Likewise, expand a drawer by animating scale or translate on a full-size child rather than interpolating the container's height, and consider content-visibility so offscreen sections do not repaint while the animation runs.
Scroll-driven animations without scroll listeners
Traditionally, reveal-on-scroll meant an IntersectionObserver toggling classes â still a fine approach, especially when you need logic alongside the motion. Native scroll-driven animations move the timeline itself onto the scroll position: animation-timeline: view() ties progress to the element's journey through the viewport, and animation-timeline: scroll() to the scroller's own position. Because the timeline is driven by scroll, no script runs per frame.
.reveal-block {
opacity: 1;
}
@supports (animation-timeline: view()) {
.reveal-block {
animation: rise-in linear both;
animation-timeline: view();
animation-range: entry 10% cover 35%;
}
.progress-rail {
position: fixed;
inset-block-start: 0;
inset-inline-start: 0;
block-size: 4px;
inline-size: 100%;
transform-origin: left;
background: #2563eb;
animation: grow-rail linear both;
animation-timeline: scroll(root block);
}
}
@keyframes rise-in {
from { opacity: 0; transform: translateY(24px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes grow-rail {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
@media (prefers-reduced-motion: reduce) {
.reveal-block { animation: none !important; }
.progress-rail { display: none; }
}
The @supports wrapper keeps older browsers on the static styles, and the reduced-motion media query short-circuits both effects for users who asked the system to calm things down.
Accessibility, user preferences and testing
Motion accessibility is a hard requirement, not a courtesy. Vestibular disorders make large parallax moves and fast scaling genuinely painful, so every animation must either respect prefers-reduced-motion or be small and non-translational enough to be safe. Never remove all feedback in the reduced-motion branch â replace motion with a static state change, as the notification example above does by jumping straight to full opacity.
Test in three passes: with animations disabled in dev tools to confirm no content is trapped behind an animation that never completes; with the OS reduced-motion setting enabled; and with the Performance panel recording a scroll, watching for long frames. If frame times spike, you are animating a layout property or triggering paint-heavy filters â switch to transform and opacity and re-measure.
Motion done well is invisible: users notice the change, not the technique. Pick transitions for state, keyframes for choreography, easings that settle instead of bounce, compositor properties throughout, and a reduced-motion branch for every effect. That checklist turns CSS animation from a decorative afterthought into a dependable communication tool.
Common Mistakes
Animating width, height, top or left for a slide or expand effect
Fix: Restructure so transform and opacity carry the motion; reserve layout properties for non-animated changes.
Shipping entrance animations with no forwards fill, so cards flash back to opacity zero
Fix: Add animation-fill-mode forwards, or animate the resting state to the visible values by default.
Forgetting the prefers-reduced-motion branch because the effect looked subtle on your monitor
Fix: Audit every animation, provide a static replacement state, and ship the media query in the same change.
Using ease-in-out for hover feedback on buttons
Fix: Favor ease-out around 150ms so the response feels immediate rather than delayed by the slow start.
Staggering list items with JavaScript timeouts that drift under load
Fix: Use nth-child animation-delay so the browser schedules the stagger on the compositor.
Running heavy blur filters on large elements during scroll
Fix: Limit filters to small surfaces, pre-render static blurs, and prefer opacity fades for scroll-driven effects.
Key Takeaways
- Transitions cover two-state changes triggered by classes or states; keyframe animations handle multi-step, repeating choreography.
- Set fill mode deliberately and always pair looping animations with a reduced-motion alternative.
- Ease-out around 150ms feels responsive; linear easing belongs to constant-rate effects like spinners.
- Transform and opacity animate on the compositor â animating layout properties is the main cause of scroll jank.
- Scroll-driven animations with animation-timeline remove per-frame JavaScript entirely, guarded by @supports.
- Test every motion effect with reduced motion enabled and with a Performance panel recording before calling it done.