Most CSS-only games lean on the Checkbox Hack — pairing an input's checked state with :checked to drive game logic. The puzzle game covered here goes the other way: it uses no checkbox at all, and still produces working drag-and-drop, a randomized grid, and shaped pieces. The build relies on CSS Grid, transitions, hover states, sibling selectors, and — for the fancy version — masks and gradients.

Faking drag and drop with hover and timing

The board itself is a straightforward CSS Grid. The harder part is making pieces seem draggable and droppable. The mechanism: hovering the empty cell makes the image move into it and stay put, even after the cursor leaves.

The persistence comes from stretching the transition. If the image is given an enormous transition duration and delay, it is technically headed back to its start position but won't get there for a very long time. Setting only transition-delay works, but large values on both delay and duration shrink the chance a player witnesses the return. With 999s + 999s — roughly 30 minutes — it does eventually move. No realistic turn took that long, so the trick holds for switching between two states.

img {
  transform: translate(200%);
  transition: 999s 999s; /* very slow move on mouseout */
}
.box:hover img {
  transform: translate(0);
  transition: 0s; /* instant move on hover */
}

Keeping the hover trigger honest

Since the image sits inside the box element, hovering the image also triggered the move — undesirable. Setting pointer-events: none on the image fixes the hover leak but kills dragging. The answer is a third element inside .box: an extra div with class .a that occupies the same grid area as the image via grid-area: 1 / 1 and serves as the hover target through a sibling selector.

.a {
  grid-area: 1 / 1;
}
img {
  grid-area: 1 / 1;
  transform: translate(200%);
  transition: 999s 999s;
}
.a:hover + img {
  transform: translate(0);
  transition: 0s;
}

Because .a covers the full box area, hovering it behaves like hovering the box, and hovering the image no longer causes problems. Dragging the image into the cell and releasing fires the hover effect, simulating a drop. Release outside the box and nothing happens.

Blocking accidental activation

A remaining flaw: hovering an empty box also produced the effect. The fix manipulates the dimensions of .a, which starts at width: 0.

.a {
  width: 0%;
  transition: 0s .2s; /* add a small delay to make sure we catch the hover effect */
}
.box:active .a { /* on :active increase the width */
  width: 100%;
  transition: 0s; /* instant change */
}
img {
  transform: translate(200%);
  transition: 999s 999s;
}
.a:hover + img {
  transform: translate(0);
  transition: 0s;
}

Clicking the image fires :active, expanding .a to full width while the pointer is held. On release inside the box, the .a element shrinks back toward width: 0 — but the hover effect fires first, and the image drops into place. Release outside the box and nothing happens.

One quirk remains: clicking the empty box moves the image, because :active is attached to .box, so clicks on the box or any of its children trigger it. Toggling pointer-events so interactions on .box are disabled while child elements stay interactive closes the gap.

.box {
  pointer-events: none;
}
.box * {
  pointer-events: initial;
}

Assembling the grid

The markup is written in Pug for convenience and compiled to plain HTML.

- let n = 4; /* number of columns/rows */
- let image = "https://picsum.photos/id/1015/800/800";

g(style=`--i:url(${image})`)
  - for(let i = 0; i < n*n; i++)
    z
      a
      b(draggable="true") 
<g style="--i: url(https://picsum.photos/id/1015/800/800)">
 <z>
   <a></a>
   <b draggable="true"></b>
 </z>
 <z>
   <a></a>
   <b draggable="true"></b>
 </z>
 <z>
   <a></a>
   <b draggable="true"></b>
 </z>
  <!-- etc. -->
</g>

The tags are arbitrary — they exist purely so the source is shorter than repeating <div class="…"> everywhere. Their roles:

  • <g> — the grid container, holding N*N <z> elements.
  • <z> — a grid item, equivalent to the .box from the drag-and-drop discussion.
  • <a> — the hover trigger.
  • <b> — a slice of the image, given the draggable attribute since it isn't draggable by default.

The container is registered as a grid in Sass, and each <z> element is made a grid itself so <a> and <b> share one grid area.

$n : 4; /* number of columns/rows */

g {
  --s: 300px; /* size of the puzzle */

  display: grid;
  max-width: var(--s);
  border: 1px solid;
  margin: auto;
  grid-template-columns: repeat($n, 1fr);
}
z {
  aspect-ratio: 1;
  display: grid;
  outline: 1px dashed;
}
a {
  grid-area: 1/1;
}
b {
  grid-area: 1/1;
}

Placing the pieces randomly is handled with a Sass function that loops over all of them. The Sass random() function yields the randomized positions, which are then disabled when the matching <b> is dragged into its cell and the <a> element is hovered. The same loop configures each piece's background: every piece shares one background-image, sized to the full grid via the --s variable, with background-position calculated so only that piece's slice shows.

b {
  background: var(--i) 0/var(--s) var(--s);
}

@for $i from 1 to ($n * $n + 1) {
  $r: (random(180));
  $x: (($i - 1)%$n);
  $y: floor(($i - 0.001) / $n);
  z:nth-of-type(#{$i}) b{
    background-position: ($x / ($n - 1)) * 100% ($y / ($n - 1)) * 100%;
    transform: 
      translate((($n - 1) / 2 - $x) * 100%, (($n - 1)/2 - $y) * 100%) 
      rotate($r * 1deg) 
      translate((random(100)*1% + ($n - 1) * 100%)) 
      rotate((random(20) - 10 - $r) * 1deg)
   }
}
z a:hover ~ b {
  transform: translate(0);
  transition: 0s;
}

Giving pieces real shapes

Same functionality, more realistic pieces, built from nine distinct shapes: four corners, four edges, and one for the interior. CSS masks combined with gradients generate the outlines. Those unfamiliar with masking should start from the simpler grid-of-pieces example, as this build extends that technique.

z  /* 0 */

z:first-child  /* 1 */

z:nth-child(-n + 4):not(:first-child) /* 2 */

z:nth-child(5) /* 3 */

z:nth-child(5n + 1):not(:first-child):not(:nth-last-child(5)) /* 4 */

z:nth-last-child(5)  /* 5 */

z:nth-child(5n):not(:nth-child(5)):not(:last-child) /* 6 */

z:last-child /* 7 */

z:nth-last-child(-n + 4):not(:last-child) /* 8 */

Eight selectors target the shape groups, and a default selector covers all pieces. The interior shape (marked 0) is the most complex: four gradients producing four curvatures — two circular bumps and two slots, plus a variable --r that controls the radius of the circular parts. The remaining shapes juggle fewer gradients; the top-edge pieces (marked 2), for instance, drop the first gradient and widen the second to cover the gap.

mask: 
  radial-gradient(var(--r) at calc(50% - var(--r) / 2) 0, #0000 98%, #000) var(--r)  
    0 / 100% var(--r) no-repeat,
  radial-gradient(var(--r) at calc(100% - var(--r)) calc(50% - var(--r) / 2), #0000 98%, #000) 
    var(--r) 50% / 100% calc(100% - 2 * var(--r)) no-repeat,
  radial-gradient(var(--r) at var(--r) calc(50% - var(--r) / 2), #000 98%, #0000),
  radial-gradient(var(--r) at calc(50% + var(--r) / 2) calc(100% - var(--r)), #000 98%, #0000);
mask: 
  radial-gradient(var(--r) at calc(100% - var(--r)) calc(50% + var(--r) / 2), #0000 98%, #000) var(--r) calc(-1 * var(--r)) no-repeat,
  radial-gradient(var(--r) at var(--r) calc(50% - var(--r) / 2), #000 98%, #0000),
  radial-gradient(var(--r) at calc(50% + var(--r) / 2) calc(100% - var(--r)), #000 98%, #0000);

Alternative gradient configurations can produce identical shapes — a more concise solution is entirely possible. Some pieces also need their width and/or height increased, since pieces must overflow their grid cells in order to connect.

height: calc(100% + var(--r));
width: calc(100% + var(--r));

Going further

Timer, completion congratulation — features like these could be layered on in a future version. The point of the exercise wasn't the game itself but the exploration: Grid, transitions, masking, gradients, selectors, background properties, and a bit of Sass for maintainability. Building a game in CSS is a demanding exercise that forces close study of individual features, and something playable falls out at the end.