---
title: "CSS Sprite Sheet Animation with steps()"
description: "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."
canonical_url: "https://spritesheetgenerator.online/guides/css-sprite-sheet-animation"
last_updated: 2026-11-01
meta:
  author: "Dennis Postma"
  "og:description": "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."
  "og:image": "https://spritesheetgenerator.online/guides/og-css-sprite-sheet-animation.png"
  "og:image:alt": "CSS Sprite Sheet Animation with steps()"
  "og:title": "CSS Sprite Sheet Animation with steps()"
  "twitter:image": "https://spritesheetgenerator.online/guides/og-css-sprite-sheet-animation.png"
  "twitter:image:alt": "CSS Sprite Sheet Animation with steps()"
---

> Index of every page on this site: [llms.txt](https://spritesheetgenerator.online/llms.txt)

## 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](https://spritesheetgenerator.online/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](https://spritesheetgenerator.online/guides/sprite-sheet-packing-padding) 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

```css
.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:

```css
.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](https://spritesheetgenerator.online/guides/pixel-perfect-scaling) 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:

```css
.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:

```css
.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:

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

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

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

```css
.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:

```css
@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:

```css
.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](https://spritesheetgenerator.online/guides/import-sprite-sheets-game-engines) 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](https://spritesheetgenerator.online/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.

[Open the editor](https://spritesheetgenerator.online/editor) [See all features](https://spritesheetgenerator.online/features)

## **Keep reading**

[**Engines****Pixel-Perfect Scaling: Fix Blurry Pixel Art** 5 min read](https://spritesheetgenerator.online/guides/pixel-perfect-scaling "Pixel-Perfect Scaling: Fix Blurry Pixel Art") [**Start here****Sprite Sheet Basics Explained** 8 min read](https://spritesheetgenerator.online/guides/sprite-sheet-basics "Sprite Sheet Basics Explained") [**Start here****Getting Started: Your First Sprite Sheet** 6 min read](https://spritesheetgenerator.online/guides/getting-started "Getting Started: Your First Sprite Sheet")

---

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://spritesheetgenerator.online/#website","@type":"WebSite","description":"Free online tool to create spritesheets for game development. Upload sprites, arrange them, and export as a spritesheet with animation preview.","name":"Spritesheet Generator","url":"https://spritesheetgenerator.online/","publisher":{"@id":"https://spritesheetgenerator.online/#identity"}},{"@id":"https://spritesheetgenerator.online/guides/css-sprite-sheet-animation#webpage","@type":"WebPage","dateModified":"2026-11-01","datePublished":"2026-10-06","description":"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.","name":"CSS Sprite Sheet Animation with steps()","url":"https://spritesheetgenerator.online/guides/css-sprite-sheet-animation","breadcrumb":{"@id":"https://spritesheetgenerator.online/guides/css-sprite-sheet-animation#breadcrumb"},"isPartOf":{"@id":"https://spritesheetgenerator.online/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://spritesheetgenerator.online/guides/css-sprite-sheet-animation"]}],"primaryImageOfPage":{"@id":"https://spritesheetgenerator.online/#logo"}},{"@id":"https://spritesheetgenerator.online/#identity","@type":"Organization","description":"A free, browser based sprite sheet generator for 2D game developers and pixel artists.","name":"Spritesheet Generator","url":"https://spritesheetgenerator.online","logo":{"@id":"https://spritesheetgenerator.online/#logo"},"sameAs":["https://discord.gg/2vvfqwn5k7"]},{"@id":"https://spritesheetgenerator.online/guides/css-sprite-sheet-animation#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Home","item":"https://spritesheetgenerator.online","position":1},{"@type":"ListItem","name":"Guides","item":"https://spritesheetgenerator.online/guides","position":2},{"@type":"ListItem","name":"CSS Sprite Sheet Animation with steps()","item":"https://spritesheetgenerator.online/guides/css-sprite-sheet-animation","position":3}]},{"@id":"https://spritesheetgenerator.online/about#dennis-postma","@type":"Person","name":"Dennis Postma","url":"https://spritesheetgenerator.online/about","sameAs":["https://github.com/Dennis4720"]},{"@id":"https://spritesheetgenerator.online/guides/css-sprite-sheet-animation#article","dateModified":"2026-11-01","datePublished":"2026-10-06","description":"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.","headline":"CSS Sprite Sheet Animation with steps()","inLanguage":"en","thumbnailUrl":"https://spritesheetgenerator.online/guides/og-css-sprite-sheet-animation.png","@type":["Article","TechArticle"],"articleSection":["Engines"],"author":{"@id":"https://spritesheetgenerator.online/about#dennis-postma"},"image":{"@id":"https://spritesheetgenerator.online/#/schema/image/1"},"isPartOf":{"@id":"https://spritesheetgenerator.online/guides/css-sprite-sheet-animation#webpage"},"mainEntityOfPage":{"@id":"https://spritesheetgenerator.online/guides/css-sprite-sheet-animation#webpage"},"publisher":{"@id":"https://spritesheetgenerator.online/#identity"}},{"@id":"https://spritesheetgenerator.online/#logo","@type":"ImageObject","caption":"Spritesheet Generator","contentUrl":"https://spritesheetgenerator.online/web-app-manifest-512x512.png","inLanguage":"en","url":"https://spritesheetgenerator.online/web-app-manifest-512x512.png"},{"@id":"https://spritesheetgenerator.online/#/schema/image/1","@type":"ImageObject","contentUrl":"https://spritesheetgenerator.online/guides/og-css-sprite-sheet-animation.png","inLanguage":"en","url":"https://spritesheetgenerator.online/guides/og-css-sprite-sheet-animation.png"}]}
```

## Sitemap

See the full [sitemap](https://spritesheetgenerator.online/sitemap.md) for all pages.
