DevAcademy
LearnCSSAnimations
AdvancedCSS

Animations

Learn how to build multi-step animations with @keyframes and the animation property.

Reading Time

18 min

Lesson

Lesson 25 of 30

Transitions vs Animations

A transition animates between two states (start and end), usually triggered by an event like :hover. A CSS animation, defined with @keyframes, can have many steps, run automatically, and repeat without any trigger at all.

A Basic Keyframe Animation

<style>
  @keyframes bounce {
    0%   { transform: translateY(0); }
    50%  { transform: translateY(-20px); }
    100% { transform: translateY(0); }
  }

  .ball {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: crimson;
    animation: bounce 1s ease-in-out infinite;
  }
</style>

<div class="ball"></div>
Output

animation Shorthand Parts

PartPurpose
nameThe @keyframes name to run
durationHow long one cycle takes
timing-functionThe speed curve, e.g. ease, linear
delayHow long to wait before starting
iteration-countHow many times to repeat, or infinite
directionnormal, reverse, alternate, or alternate-reverse

Using Percentages in @keyframes

@keyframes can define as many steps as needed using percentages from 0% to 100%, giving fine control over multi-stage animations. from and to are shorthand for 0% and 100%.

Multi-Step Animation

<style>
  @keyframes pulse-color {
    0%   { background: steelblue; }
    50%  { background: #764ba2; }
    100% { background: steelblue; }
  }

  .banner {
    padding: 20px;
    color: white;
    text-align: center;
    animation: pulse-color 2s ease-in-out infinite;
  }
</style>

<div class="banner">Pulsing background</div>
Output

Controlling Playback

animation-iteration-count controls how many times the animation runs (a number, or infinite), and animation-direction can alternate the animation back and forth instead of resetting each cycle.

Alternating Direction

.icon {
  animation: spin 3s linear infinite alternate;
}

animation-fill-mode

By default, an element reverts to its original styles after an animation ends. animation-fill-mode: forwards keeps the styles from the last keyframe instead, useful for entrance animations that shouldn’t reset.

Respect Reduced Motion

Some users prefer reduced motion due to vestibular disorders. Wrap non-essential animations in @media (prefers-reduced-motion: no-preference) so they’re skipped for users who have requested reduced motion in their OS settings.

Best Practice

Use transitions for simple, event-triggered state changes, and reserve @keyframes animations for looping or multi-step effects that need to run independently of user interaction.

Interview Questions

Quick Quiz

1. What is the main difference between a transition and an animation?

2. Which at-rule defines the steps of a CSS animation?

3. Which media feature should wrap non-essential animations for accessibility?