What is the difference between @keyframes and transition?▾
Transitions animate between two states triggered by a user action or state change. @keyframes define multi-stop animations that run on their own timeline, can loop, reverse, delay, and play without any trigger. Use transitions for interactive state changes and keyframes for autonomous, orchestrated motion.
How do I make an animation play once and stop at the end?▾
Set animation-iteration-count: 1 (the default) and animation-fill-mode: forwards. The forwards fill mode keeps the element in the state defined at the final keyframe after the animation completes, preventing it from snapping back to its initial state.
What does animation-fill-mode: forwards do?▾
forwards tells the browser to keep the element in the state of the last keyframe after the animation finishes. Without it, the element snaps back to its pre-animation CSS values, which makes entrance animations suddenly disappear after playing.
How do I pause animation on hover?▾
Add animation-play-state: paused to the :hover rule on the animated element. The animation freezes in place when the cursor enters the element and resumes from the same point when the cursor leaves.
Can I run multiple animations on the same element?▾
Yes. The animation property accepts a comma-separated list of animation values. Each entry in the list corresponds to a separate @keyframes name with its own duration, easing, delay, and iteration count. Multiple animations compose additively on the element.
Should I animate transform or top/left for performance?▾
Always animate transform and opacity when possible. These properties are GPU-composited and do not trigger layout or paint. Animating top, left, width, height, or any layout-affecting property causes layout recalculation on every frame, producing janky animations on complex pages.
What is a cubic-bezier easing curve?▾
cubic-bezier() defines a custom easing curve using four control-point values between 0 and 1. The built-in keyword easings like ease and ease-in-out map to specific cubic-bezier values. Custom curves let you define spring-like overshoots, immediate starts, or exaggerated decelerations.
How do I respect prefers-reduced-motion in animations?▾
Wrap your animation declarations in @media (prefers-reduced-motion: no-preference) { } so they only apply when the user has not requested reduced motion. For motion-critical UIs, provide a static alternative in @media (prefers-reduced-motion: reduce) that conveys the same information without movement.