CSS Animations & Sprite Sheets
@keyframes, the animation properties, and sprite-sheet animation with steps().
What is a CSS Animation?
- Keyframe-based —
@keyframesdefines the animation. Specify styles at 0%, 50%, 100%, or use thefrom/toshorthand. - Bound to elements —
animation-namelinks to a@keyframesrule; 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
| Property | Purpose |
|---|---|
@keyframes | Defines the animation sequence with style steps |
animation-name | Links the element to the @keyframes rule name |
animation-duration | How long one cycle takes (e.g. 1s, 300ms) |
animation-delay | Wait before starting (negative = skip ahead) |
animation-iteration-count | Number of cycles: 1, 3, infinite |
animation-direction | normal | reverse | alternate | alternate-reverse |
animation-timing-function | Speed curve: ease | linear | steps(n) | cubic-bezier() |
animation-fill-mode | Style before/after: none | forwards | backwards | both |
@keyframes — Defining the Animation
- Use
from/tofor simple two-state animations. - Use percentages (0%, 50%, 100%) for multi-step sequences.
- The rule name is referenced by
animation-name; multiple@keyframescan 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 —
-2sstarts 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.5are 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
| Value | Behaviour |
|---|---|
normal | Plays forwards (0% → 100%). Default. |
reverse | Plays backwards (100% → 0%). |
alternate | Forwards, then backwards on the next cycle. |
alternate-reverse | Backwards 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 throughoutease-in/ease-out/ease-in-outcubic-bezier(x1,y1,x2,y2)— custom curvesteps(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
| Value | Behaviour |
|---|---|
none | Default — reverts to original styles before and after. |
forwards | Keeps the final keyframe styles after the animation ends. |
backwards | Applies first keyframe styles during the animation-delay wait. |
both | Applies 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: ;}