Skip to main content

CSS Keyframe Animations

While transitions animate between two states, keyframe animations let you define complex, multi-step sequences. They can run automatically, repeat indefinitely, and create effects that transitions simply cannot.

The @keyframes Syntax

Define an animation sequence with @keyframes, then apply it with the animation property:

Ctrl+Enter
HTML
CSS
JS
Preview

For multi-step animations, use percentage keyframes:

Ctrl+Enter
HTML
CSS
JS
Preview

You can list multiple stops at the same percentage to hold a value:

Ctrl+Enter
HTML
CSS
JS
Preview

Animation Properties

The animation shorthand combines multiple properties:

.element {
  animation: bounce 0.6s ease-in-out 0.2s infinite alternate both;
  /*        name   duration timing    delay count   direction fill */
}

Here is what each property controls:

PropertyValuesPurpose
animation-namekeyframe nameWhich animation to run
animation-durationtime (e.g., 0.3s)How long one cycle takes
animation-timing-functionease, linear, cubic-bezierSpeed curve
animation-delaytimeWait before starting
animation-iteration-countnumber or infiniteHow many times to repeat
animation-directionnormal, reverse, alternateDirection of playback
animation-fill-modenone, forwards, backwards, bothWhat styles apply before/after
animation-play-staterunning, pausedPlay or pause

Fill Modes

Fill mode is critical for one-shot animations. Without it, the element snaps back to its original state when the animation ends:

Ctrl+Enter
HTML
CSS
JS
Preview

Use forwards when the animation should stick. Use both when there is a delay and the element should start in the first keyframe state.

Practical Animations

Loading Spinner

Ctrl+Enter
HTML
CSS
JS
Preview

Skeleton Screen Shimmer

Ctrl+Enter
HTML
CSS
JS
Preview

Staggered List Entry

Ctrl+Enter
HTML
CSS
JS
Preview

Staggering creates a cascading effect that feels natural and draws the eye down the list. Keep delays short (30-80ms between items) to avoid feeling sluggish.

Notification Entry

Ctrl+Enter
HTML
CSS
JS
Preview

The slight overshoot at 60% creates a natural deceleration that catches the user's eye.

Choreography

When multiple elements animate, their timing relationships matter. Choreography is the art of making multiple animations feel coordinated:

Ctrl+Enter
HTML
CSS
JS
Preview

Rules for good choreography:

  • Container before content — the backdrop or wrapper should appear before its children
  • Large movements before small ones — primary elements animate first
  • Keep total sequence under 500ms — users should not wait for animations to finish
  • Exit animations are faster than entries — when closing, users want it gone quickly

Reducing Motion

Always provide a reduced-motion alternative:

Ctrl+Enter
HTML
CSS
JS
Preview

This global reset disables all animations for users who request it while still allowing animation-fill-mode: forwards to apply final states instantly.