Aduok Code

A Pixel-Art Flapping Bat, Built from One 1px Element, Thousands of box-shadow Pixels, Seven Hand-Drawn Keyframes, and a steps(1) Timing Function That Flips Frames Like a Flipbook

Introduction

This bat is one empty div, a few lines of CSS, and no image. It is drawn in pixel art, a dark body with a soft blue-grey outline and two white eyes, and it flaps its wings in a loop. Every pixel you see is a single entry in a box-shadow list, and every wing position is a keyframe that holds a different list. Change three color values and the bat is a different bat. Change one number and it is twice as big.

The animation rests on four ideas: an element that is exactly one pixel large and used as a brush, a box-shadow that copies that pixel to any offset in any color, a transform that blows the tiny sprite up to a visible size, and a keyframe animation with a steps(1) timing function so the frames switch instantly instead of blending.

What you will learn

How a 1px element can paint a whole picture with box-shadow — how to keep colors in variables so every frame shares them — how to enlarge pixel art with transform: scale — how to centre a sprite whose shadows spill outside its box — how steps(1) makes keyframes behave like flipbook pages — and how to respect users who ask for less motion.

The sprite data is generated, not typed

The file holds about 7,700 pixel entries. Nobody types that by hand. The pixel lists come from a pixel editor or a small script that reads a grid and prints one entry per filled cell. This post explains how the entries work, so you can draw your own sprite and output it the same way.

01
Part One
A 1px Element as a Pixel Brush

Step 1 — The Whole Element Is One Pixel

The HTML is a single div with the class bat. In CSS it is given a width of 1px and a height of 1px. By itself it draws nothing visible, because it has no background. Its only job is to be the origin: a one-pixel point that box-shadow can copy outward from.

HTML
<div class="bat" role="img" aria-label="Animated pixel-art bat"></div>
CSS
.bat {
  position: relative;
  width: 1px;
  height: 1px;
}

A shadow has no size of its own. It copies the shape of its element. Make the element one pixel and every shadow is one pixel.

02
Part Two
One box-shadow Entry per Pixel

Step 2 — Reading a box-shadow as Coordinates

A box-shadow entry is written as an x offset, a y offset, and a color. With no blur and no spread, the shadow is an exact copy of the element moved by that offset. Since the element is 1px by 1px, the entry 35px 7px red means: put a red pixel 35 pixels to the right and 7 pixels down from the origin. A comma-separated list of entries is a list of pixels, and a list of enough pixels is a picture.

CSS
/* a tiny three-pixel "L" shape */
.demo {
  width: 1px;
  height: 1px;
  box-shadow:
    0px 0px #fff,
    0px 1px #fff,
    1px 1px #fff;
}

The bat uses the same idea at scale. Its sprite sits on a grid about 64 by 64 cells, so offsets run from 0 to about 63 in both directions. Each filled cell becomes one entry, and the list is sorted row by row, from the top of the sprite to the bottom, which makes it easy to read. A row near the top of the bat looks like this.

CSS
33px 7px $bat-outline,
34px 7px $bat-outline,
35px 7px $bat-body,
36px 7px $bat-body,
37px 7px $bat-outline,
38px 7px $bat-outline,
39px 7px $bat-outline

Read it left to right: on row 7, the pixels from x = 33 to 39 are outline, outline, body, body, outline, outline, outline. That is a small piece of the bat's ear. Empty cells are simply left out, which is why the transparent background costs nothing.

03
Part Three
Three Colors, Named Once

Step 3 — Outline, Body, and Eyes

The sprite uses only three colors, and each is stored in a variable instead of being repeated as a hex code thousands of times. The outline is a soft blue-grey, the body is near black, and the eyes are white. Every frame refers to the variables, so changing the palette is a three-line edit.

SCSS
$bat-outline: #54556b;
$bat-body: #202020;
$bat-eyes: #fff;
Name colors by role, not by position

The first version of this file called the colors a, b and c. That works, but it tells the next reader nothing. Names like bat-outline and bat-body describe what the color is for, so someone can restyle the bat without reading a single pixel entry.

In plain CSS the same job is done with custom properties. Declare --bat-outline, --bat-body and --bat-eyes on the root, and write var(--bat-outline) inside the shadow list. A custom property is allowed anywhere a color is, including inside box-shadow, so the single-file HTML version works with no build step.

CSS
:root {
  --bat-outline: #54556b;
  --bat-body: #202020;
  --bat-eyes: #fff;
}

.bat {
  box-shadow:
    33px 7px var(--bat-outline),
    35px 7px var(--bat-body),
    20px 37px var(--bat-eyes);
}
04
Part Four
Scaling and Centering the Sprite

Step 4 — transform: scale Makes Each Pixel Bigger

A sprite drawn at one real pixel per cell would be about 60 screen pixels wide, which is tiny. Scaling the origin element by 4 scales its shadows with it, so each cell becomes a crisp 4 by 4 square. The scale is kept in a variable, so you can make the bat bigger by changing one number. An integer scale keeps the edges sharp. A fractional one blurs the pixels.

There is one catch. The shadows are drawn to the right of and below the origin, so the origin is not the centre of the picture. It is the top-left corner. Left alone, the bat would hang off to the lower right of the page. To fix that, the element is nudged back by half the sprite size multiplied by the scale: 32 times 4 is 128 pixels up and to the left.

SCSS
$bat-scale: 4;
$bat-sprite-origin: 32px; // half of the 64-cell sprite grid

.bat {
  position: relative;
  top: -$bat-sprite-origin * $bat-scale;
  left: -$bat-sprite-origin * $bat-scale;
  width: 1px;
  height: 1px;
  transform: scale($bat-scale);
}

The page itself only has to centre the origin: a flex body with align-items and justify-content set to center, on a full-height html and body. The sprite then stretches away from that point and the offset pulls it back so the middle of the bat lands in the middle of the window. Because the compensation is calculated from the same two variables, it stays correct when you change the scale.

CSS
html,
body {
  height: 100%;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
}
05
Part Five
Keyframes as Sprite Frames

Step 5 — Each Keyframe Holds a Full Picture

Normally a keyframe changes a few properties and the browser blends between them. Here every keyframe replaces the entire box-shadow list with a different picture of the bat: wings up, wings level, wings down, and the positions in between. The file has eight keyframe blocks, at 0%, 14.3%, 28.6%, 42.9%, 57.2%, 71.5%, 85.8% and 100%. That spacing is one divided by seven, so the frames are evenly timed.

SCSS
@keyframes bat-flap {
  0% {
    box-shadow:
      33px 6px $bat-outline,
      34px 6px $bat-outline,
      /* ... about 1,100 more entries ... */
      21px 51px $bat-body;
  }

  14.3% {
    box-shadow:
      17px 7px $bat-outline,
      37px 7px $bat-outline,
      /* ... about 1,000 more entries ... */
  }

  /* 28.6%, 42.9%, 57.2%, 71.5%, 85.8%, 100% ... */
}

The frames differ a lot in size because the wings move through very different shapes. The tallest frame has around 1,100 pixels and the thinnest, with the wings folded, has about 700. Together the eight blocks hold around 7,750 pixels, which is why the stylesheet is large even though the idea is simple.

The last frame is nearly invisible

With steps(1), the 100% keyframe is only reached at the very end of the cycle, and the animation then loops straight back to 0%. In practice you see seven frames held for a seventh of the cycle each, and the 100% frame appears for an instant. If you build your own sprite, either make 100% match 0% for a clean loop or leave it out.

06
Part Six
Why steps(1) Is the Key

Step 6 — Frames That Switch Instead of Blend

Left on the default easing, the browser would try to interpolate between two box-shadow lists. When the lists have different numbers of entries it cannot match them up cleanly, so the result is a smear or a jump. The timing function steps(1) removes the problem. It holds each keyframe's value for the whole segment and then switches to the next value at once, which is exactly how a flipbook works.

SCSS
$bat-frame-duration: 0.4s;

.bat {
  animation: bat-flap $bat-frame-duration steps(1) infinite;
}

The shorthand reads as: use the keyframes named bat-flap, take 0.4 seconds for one full cycle, use one step per keyframe segment, and repeat forever. Seven visible frames in 0.4 seconds is about 17 frames per second, which suits pixel art: fast enough to read as a flap, choppy enough to look hand-animated. Raise the duration to slow the bat down, or lower it to make it frantic.

steps(1) turns an animation from a blend into a flipbook. Every keyframe becomes a page.

07
Part Seven
Naming, Motion Preferences, and Accessibility

Step 7 — Small Changes That Make It Production-Ready

Three small details lift a toy into something you can ship. First, the keyframes are named bat-flap and not bat, so the animation name no longer collides with the class name. Second, a prefers-reduced-motion rule turns the animation off for anyone who has asked their system for less movement, leaving the first frame as a still picture. Third, the div is marked as an image with a label, so a screen reader announces one bat instead of an empty box.

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

There is also a performance note. A box-shadow list with thousands of entries is expensive to repaint, and this animation changes the list every frame. On a small screen with one bat that is fine. If you put dozens of bats on a page, draw the sprite once to a canvas or an image sprite sheet and animate with background-position instead.

Draw your own bat

Make a 64 by 64 grid in any pixel editor, draw one wing position per frame in three colors, and export each frame as a list of x, y, color triples. A ten-line script can turn each list into a box-shadow block. Paste the blocks into keyframes spaced evenly from 0% to 100%.

Tuning Reference

ValueExampleEffect
$bat-scale4Size of each pixel on screen; use whole numbers for sharp edges
$bat-frame-duration0.4sTime for one full flap cycle; lower is faster
$bat-sprite-origin32pxHalf of the sprite grid, multiplied by the scale to centre the bat
$bat-outline#54556bColour of the edge pixels around the body and wings
$bat-body#202020Colour of the fill inside the outline
$bat-eyes#fffColour of the eye pixels
Timing functionsteps(1)Switches frames instantly instead of blending them
Keyframe spacing14.3%One divided by seven; keeps every visible frame the same length
Element size1px by 1pxMakes every shadow a single pixel

Full Source Code

The finished bat is a single self-contained HTML file with its markup, styles and animation together and no build step. It uses CSS custom properties instead of SCSS variables, so it opens directly in any current browser. The pixel data makes the file about 290 KB, almost all of it the eight box-shadow lists.

HTML — pixel-bat.html (complete)
// Full source available in the Aduok GitHub repository
// https://github.com/aduok/aduok-code-snippets/blob/main/blog/pixel-bat.html
Want to try it yourself?Open the live pixel-bat.html file