All guides
CSS Sprite Sheet Animation with steps()
IntermediateEngines7 min readUpdated November 1, 2026

CSS Sprite Sheet Animation with steps()

Play a sprite sheet on a web page with plain CSS: steps(), the end-position off-by-one, sharp pixel art scaling, and picking an animation row with a class.

Written by Dennis Postma
Game developer, maintainer of Spritesheet Generator

TL;DR

Make a box one frame wide, put the whole strip in its background, and animate background-position-x with steps(). The background then jumps one frame at a time instead of sliding. Export the animation as a single row, set the box to one frame's size, and add image-rendering: pixelated if you scale it up. No JavaScript and no canvas.

Why Use CSS at All

Most sprite sheets go into a game engine. Some go onto a web page instead: a mascot on a landing page, a loading indicator, a character pacing on a 404 page.

A canvas and a game loop is a lot of machinery for that. CSS plays a sprite sheet in about ten lines, the browser handles the timing, and it keeps working with JavaScript disabled.

How It Works

Take eight 32x32 frames laid side by side in a 256x32 image. Put a 32x32 window over the first frame. Move the image 32 pixels left and the window shows frame two; move it again and you get frame three.

The window is your element and the image is its background. A normal CSS animation would slide the background smoothly, so you would see the strip dragging past. steps(8) makes it move in eight jumps and hold still between them, one jump per frame.

Step 1: Export a Strip

One row is the easiest layout to animate. Load your frames in the editor, set Columns to the number of frames, and set Padding to 0.

Padding in this editor adds transparent pixels on each side of every frame, so with 2 px of horizontal padding a 32 px frame sits in a 36 px cell. The CSS still works if you use 36 everywhere, but the box then includes the empty border, and it is easy to type 32 by habit. With padding at 0, the cell size is the art size. (Padding exists for game engines that filter textures; the packing guide explains that case.)

Export PNG. Eight 32x32 frames give you a 256x32 file.

Tip

Play the animation in the Preview tab first and find the Speed (FPS) value that looks right. You need that number for the duration below, and judging it there is quicker than editing CSS and reloading.

Step 2: The Ten Lines

.hero {
  width: 32px;
  height: 32px;
  background: url('hero-walk.png') no-repeat 0 0;
  animation: walk 0.8s steps(8) infinite;
}

@keyframes walk {
  to {
    background-position-x: -256px;
  }
}

Put <div class="hero"></div> on a page and the character walks in place.

Duration is frames divided by frame rate. Eight frames at 10 fps is 0.8 seconds. Some combinations to start from:

Frames FPS Duration Typical use
4 8 0.5s Slow idle
6 12 0.5s Quick walk
8 10 0.8s Relaxed walk
8 12 0.667s Run
12 24 0.5s Fire or smoke effect

The Off-by-One That Shows the Wrong Frame

The strip is 256 pixels wide and the last frame starts at x = 224. So why does the keyframe go to -256?

Because the default steps() mode jumps at the end of each interval. With steps(8) the background sits at 0, -32, -64 and so on down to -224, each for an eighth of the duration. The value -256 would only show at the instant the cycle ends, which is the instant it restarts at 0, so you never see it.

Get this wrong in one of two ways. Animate to -224 with steps(8) and every step is 28 pixels instead of 32, so you see half of one pose next to half of the next. Animate to -224 with steps(7) and the positions are right but the last frame is skipped.

The rule: animate to -(frames x frame width), the full width of the strip, with steps(frames). The end position is one frame past the last one, and that is correct.

Scaling Pixel Art Without Blur

32x32 is tiny on a modern screen. To show it at 128x128, make the box four times bigger and scale the background with it:

.hero {
  width: 128px;
  height: 128px;
  background: url('hero-walk.png') no-repeat 0 0 / 1024px 128px;
  image-rendering: pixelated;
  animation: walk 0.8s steps(8) infinite;
}

@keyframes walk {
  to {
    background-position-x: -1024px;
  }
}

Without image-rendering: pixelated the browser smooths the upscale and the edges go soft. With it, each source pixel becomes a clean 4x4 block. Use whole-number scales (2x, 3x, 4x). At 2.5x some source pixels become two screen pixels wide and others three, and the sprite shimmers while it animates. The pixel-perfect scaling guide goes into why.

A Version That Works at Any Size

With -1024px hard-coded, every scale change means editing the keyframe. Percentages avoid that. background-position-x: 100% lines up the right edge of the image with the right edge of the box, which is where the last frame is, whatever the size:

.hero {
  width: 128px;
  height: 128px;
  background: url('hero-walk.png') no-repeat 0 0 / 800% 100%;
  image-rendering: pixelated;
  animation: walk 0.8s steps(8, jump-none) infinite;
}

@keyframes walk {
  to {
    background-position-x: 100%;
  }
}

800% 100% makes the background eight boxes wide and one box tall. jump-none makes steps() include both the first and the last value, so eight steps land on eight frames from 0% to 100% and nothing past the end. Change the box to 64px or 256px and nothing else needs to change.

Switching Animations With a Class

Characters usually have several animations. Put them on one sheet, one row each, all the same width:

Row Animation y offset (32px frames)
1 Idle 0
2 Walk -32px
3 Jump -64px

Animate only the x axis and choose the row with a class:

.hero {
  width: 32px;
  height: 32px;
  background: url('hero.png') no-repeat 0 0;
  animation: cycle 0.8s steps(8) infinite;
}

.hero.is-walking {
  background-position-y: -32px;
}

.hero.is-jumping {
  background-position-y: -64px;
}

@keyframes cycle {
  to {
    background-position-x: -256px;
  }
}

That is the reason the keyframe uses background-position-x and not background-position. The shorthand sets both axes, so the animation would override the row the class picked and pull it back to the top.

This only works if every row has the same frame count. If idle has four frames and walk has eight, give each class its own animation with its own steps() and end position. When you build the sheet, set Columns to the length of the longest animation so every row starts at x = 0.

Play, Pause, and Reduced Motion

Play on hover only:

.hero {
  animation-play-state: paused;
}

.hero:hover {
  animation-play-state: running;
}

Play once and hold the last frame, for something like a chest opening:

.chest.is-open {
  animation: open 0.5s steps(6, jump-none) forwards;
}

@keyframes open {
  to {
    background-position-x: -160px; /* last frame of a 6-frame, 32px strip */
  }
}

forwards keeps the final keyframe value after the animation ends, which breaks the earlier rule. With the default step mode and an end position one frame past the strip, the element would freeze on empty space. For one-shot animations, end on the last frame itself (-(frames - 1) x width) and use jump-none so that frame gets its full share of time.

For people who have asked their OS for less motion:

@media (prefers-reduced-motion: reduce) {
  .hero {
    animation: none;
  }
}

The first frame stays visible, so the character is still on the page, just standing still.

What the CSS Export Is For

The Format button in the Export panel includes CSS Sprites. It writes one class per frame:

.sprite {
  display: inline-block;
  background: url('spritesheet.png') no-repeat;
  width: 32px;
  height: 32px;
}
.sprite-0 {
  background-position: -0px -0px;
}
.sprite-1 {
  background-position: -32px -0px;
}
.sprite-2 {
  background-position: -64px -0px;
}

This is the older CSS sprite technique: one image request for a set of icons, each class showing one of them. It suits static frames such as UI icons, inventory slots, or a health bar with five states, and cases where JavaScript picks the frame, like one frame per scroll position. For a looping animation, the steps() version above is less code.

When CSS Is the Wrong Tool

Animating background-position repaints the element at every frame change. For one mascot, or ten, you will not notice. For a hundred sprites moving around at once you are writing a game, and a canvas renderer like Phaser or PixiJS is the better fit. The engine import guide covers that route.

If you need many CSS sprites, there is a middle option: put an <img> of the strip inside a box with overflow: hidden and animate transform: translateX() with steps() instead. Transforms are usually handled by the compositor, so the browser can skip the repaint. The math is the same.

Checklist

  • One row, padding 0, all frames the same size
  • steps(frames) to the full strip width, or steps(frames, jump-none) to 100%
  • Duration = frames / fps
  • image-rendering: pixelated and whole-number scales
  • Animate background-position-x so a class can pick the row
  • A prefers-reduced-motion rule

Lay out the strip, check the timing in Preview, export the PNG. Open the editor

Try it in the editor

The editor is free and needs no account. Open it in another tab and work through the steps above with your own frames.

Keep reading