Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14
Chapter 3

CSS Animations & Sprite Sheets

@keyframes, the animation properties, and sprite-sheet animation with steps().


What is a CSS Animation?

  • Keyframe-based — @keyframes defines the animation. Specify styles at 0%, 50%, 100%, or use the from / to shorthand.
  • Bound to elements — animation-name links to a @keyframes rule; runs when the element is visible and can be triggered by JavaScript.
  • Fully controllable — duration, delay, iteration, direction, timing, fill mode; pause, play and stop.

CSS animations change any CSS property over time — no JavaScript needed for visual effects.

CSS Animation Properties

PropertyPurpose
@keyframesDefines the animation sequence with style steps
animation-nameLinks the element to the @keyframes rule name
animation-durationHow long one cycle takes (e.g. 1s, 300ms)
animation-delayWait before starting (negative = skip ahead)
animation-iteration-countNumber of cycles: 1, 3, infinite
animation-directionnormal | reverse | alternate | alternate-reverse
animation-timing-functionSpeed curve: ease | linear | steps(n) | cubic-bezier()
animation-fill-modeStyle before/after: none | forwards | backwards | both

@keyframes — Defining the Animation

  • Use from / to for simple two-state animations.
  • Use percentages (0%, 50%, 100%) for multi-step sequences.
  • The rule name is referenced by animation-name; multiple @keyframes can exist in one stylesheet.
/* Simple two-state */
@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* Multi-step */
@keyframes slideAndFade {
  0%   { transform: translateY(-20px); opacity: 0; }
  50%  { opacity: 1; }
  100% { transform: translateY(0); opacity: 1; }
}

animation-name & animation-duration

.element {
  animation-name: enemy1;
  animation-duration: 0.8s;   /* REQUIRED — default is 0s (no animation) */
}

/* Multiple animations: comma-separated names and durations */
.hero-card {
  animation-name: fadeIn, float;
  animation-duration: 0.5s, 3s;
}

animation-delay

  • Positive value: the animation waits N seconds before starting.
  • Negative value: starts as if it has already been running — -2s starts 2 seconds in.
  • Useful for staggered effects on list items.
.element   { animation-name: sprite-anim; animation-duration: 4s; }
.element-2 { animation-name: sprite-anim; animation-duration: 4s; animation-delay: -2s; }

/* Staggered list animation */
.item:nth-child(1) { animation-delay: 0.1s; }
.item:nth-child(2) { animation-delay: 0.2s; }
.item:nth-child(3) { animation-delay: 0.3s; }

animation-iteration-count

  • Default: 1 — plays once then stops.
  • infinite — loops forever (great for sprite animations).
  • A number plays exactly that many times; decimals like 1.5 are allowed.
.sprite { animation-name: sprite-loop; animation-duration: 4s; animation-iteration-count: infinite; }
.bounce { animation-name: bounceIn; animation-duration: 0.6s; animation-iteration-count: 3; }

animation-direction

ValueBehaviour
normalPlays forwards (0% → 100%). Default.
reversePlays backwards (100% → 0%).
alternateForwards, then backwards on the next cycle.
alternate-reverseBackwards first, then forwards on the next cycle.
.pendulum {
  animation-name: swing;
  animation-duration: 1.5s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: ease-in-out;
}

animation-timing-function

  • ease — slow start, fast middle, slow end (default)
  • linear — constant speed throughout
  • ease-in / ease-out / ease-in-out
  • cubic-bezier(x1,y1,x2,y2) — custom curve
  • steps(n) — discrete jumps — essential for sprite sheets (e.g. steps(6) = 6 frames per cycle)
#sprite-image {
  animation-name: enemy1;
  animation-duration: 0.8s;
  animation-timing-function: steps(6);   /* 6 frames in the sprite strip */
}

animation-fill-mode

ValueBehaviour
noneDefault — reverts to original styles before and after.
forwardsKeeps the final keyframe styles after the animation ends.
backwardsApplies first keyframe styles during the animation-delay wait.
bothApplies backwards before and forwards after — most flexible.
.fly-in {
  animation-name: slideIn;
  animation-duration: 0.5s;
  animation-delay: 0.3s;
  animation-fill-mode: forwards;   /* without it, the element snaps back! */
}

Animation Shorthand

/* Individual properties... */
.sprite {
  animation-name: enemy1;
  animation-duration: 0.8s;
  animation-timing-function: steps(6);
  animation-delay: 0;
  animation-iteration-count: infinite;
  animation-direction: normal;
}

/* ...same thing as one shorthand */
.sprite { animation: enemy1 0.8s steps(6) 0 infinite normal; }

/* Multiple animations — comma separated */
.hero { animation: fadeIn 0.4s ease forwards, float 3s ease-in-out 0.4s infinite alternate; }

Complete Sprite Animation Example

enemy1.png is a sprite strip containing 6 animation frames. steps(6) jumps between them while background-position slides from 0px to -1754px — producing a walking character loop.

/* sprite3.css */
#sprite-image {
  height: 155px;
  width: 295px;
  background: url("enemy1.png") 0px 0px;
  animation: enemy1 0.8s steps(6) 0 infinite normal;
}

@keyframes enemy1 {
  100% { background-position: -1754px; }
}
<!doctype html>
<html>
<head>
  <title>CSS Sprite Animation</title>
  <link rel="stylesheet" href="sprite3.css">
</head>
<body>
  <h1>CSS Animation</h1>
  <div id="sprite-container">
    <div id="sprite-image"></div>
  </div>
</body>
</html>

Activity — complete the code

Complete the code correctly to mark this lesson as complete automatically.

Complete the keyframes and animation properties.

@ walk {
to { background-position: -300px 0; }
}
 
.strip {
animation-name: ;
animation-duration: 0.9s;
animation-timing-function: (3);
animation-iteration-count: ;
}