What is @keyframes in CSS?
@keyframes defines the stages of a CSS animation. Each stage is a percentage or from/to selector with the styles that apply at that point. When the animation runs, the browser interpolates between those stages, so a 0% opacity of 0 and a 100% opacity of 1 fades the element in.
How do I write a keyframes animation?
Write the @keyframes rule with a name, add at least the start and end stages, then attach it with the animation shorthand: animation: 1000ms ease-in-out 1 normal both my-animation;. The shorthand order is duration, timing function, iteration count, direction, fill mode and name.
What does animation-fill-mode do?
animation-fill-mode decides which keyframe styles apply outside the animation's active time. forwards keeps the 100% styles after it ends, backwards applies the 0% styles during a delay, both applies both, and none returns the element to its own styles once the animation finishes.
Can a keyframes animation have more than two stops?
Yes. Add as many percentage stops as the motion needs, such as 0%, 25%, 50%, 75% and 100%. Each stop can change opacity, translate, scale and rotate independently. Keep offsets in ascending order for CSS that is easy to read, though the browser applies rules by percentage rather than source order.
How do I respect reduced motion?
Wrap the animation in an @media (prefers-reduced-motion: reduce) block and set animation: none, or reduce the movement to a simple fade. The live preview on this page already pauses when the visitor's operating system asks for reduced motion.
What animation duration should I use for UI motion?
Most interface animations feel right between 150ms and 400ms. Shorter times suit small hover and press feedback, while larger entrances, drawers and page transitions often sit around 300ms to 500ms. Longer durations work for decorative loops, but keep repeated motion subtle.