Using :target as lightweight client-side routing
Most CSS game hacks lean on checkbox and radio tricks or on pseudo-classes like :hover and :valid. The :target pseudo-class is a less-explored option, yet it lets a stylesheet react to whichever jump link is currently selected — effectively giving the browser a primitive client-side router with no JavaScript.
That property is what makes a fully playable Tic Tac Toe opponent possible in pure CSS. Because Tic Tac Toe has only 5,478 legal states reachable when X moves first, and an optimal move can be computed for any of them, the entire game tree can be enumerated and linked together.
Why some games fit in CSS and others don't
CSS hacks work by switching between selectors driven by user input and showing or hiding pre-built markup. The practical ceiling is the size of the state space: a 4×4 Sudoku solver is feasible, while a 9×9 version is close to impossible.
For the Tic Tac Toe build, the intelligence lives in the HTML rather than the stylesheet. A modified minimax algorithm written in Ruby pre-generates every reachable state, with each empty square linking to the computer's optimal reply. Because CodePen supports HAML, which supports Ruby blocks, it can double as a hidden Ruby environment for generating the markup.
Every generated state follows this pattern in the document:
<div class="b" id="--OOX----">
<svg class="o s">
<circle></circle>
</svg>
<a class="s" href="#OXOOX----">
<div></div>
</a>
<svg class="o s">
<circle class="c"></circle>
</svg>
<svg class="o s">
<circle class="c"></circle>
</svg>
<div class="x"></div>
<a class="s" href="#O-OOXX---">
<div></div>
</a>
<a class="s" href="#O-OOX-X--">
<div></div>
</a>
<a class="s" href="#O-OOX--X-">
<div></div>
</a>
<a class="s" href="#O-OOX---X">
<div></div>
</a>
</div>
Showing one board at a time
The CSS itself is straightforward: the state selected by the jump link is displayed via :target, and a .c class marks historical computer moves so the handwriting animation only fires for the latest one. Visually the player seems to sit on a single board; underneath, they are jumping between sections of the document.
/* Game's parent container */
.b, body:has(:target) #--------- {
/* Game states */
.s {
display: none;
}
}
/* Game pieces with :target, elements with href */
:target, #--------- {
width: 300px;
height: 300px; /
left: calc(50vw - 150px);
top: calc(50vh - 150px);
background-image: url(/path/to/animated/grid.gif);
background-repeat: no-repeat;
background-size: 100% auto;
/* Display that game state and bring it to the forefront */
.s {
z-index: 1;
display: inline-block;
}
/* The player's move */
.x {
z-index: 1;
display: inline-block;
background-image: url("data:image/svg+xml [...]"); /** shortened for brevity **/
height: 100px;
width: 100px;
}
/* The browser's move */
circle {
animation-fill-mode: forwards;
animation-name: draw;
animation-duration: 1s;
/* Only animate the browser's latest turn */
&.c {
animation-play-state: paused;
animation-delay: -1s;
}
}
}
Clicking an empty square selects its jump link, and :target then displays the updated state (.s), with the computer's precomputed response (animated via .c) arriving as a styled entrance.
The opening position is a special case: before any link is selected there is nothing for :target to match, so the initial empty grid is shown by default and hidden once play begins using :body:has(:target) #---------. Any :target-based experiment needs the same treatment so visitors see something before they interact.
What :target buys you over the checkbox hack
The checkbox hack can produce the same game, but :target carries advantages specific to URL-driven state:
- Persistent games. Bookmark the URL and return in exactly the state you left.
- Browser history as a control scheme. Back undoes a move; Forward replays it. Combined with the checkbox hack, this suggests games built around a time-travel mechanic in the spirit of Braid.
- Shareable states. A win or a draw against the unbeatable algorithm can be broadcast simply by sharing the URL, with Wordle-style bragging potential.
- More semantic markup. The checkbox hack requires hiding inputs, which is a hack with real accessibility trade-offs; this approach uses jump links and divs and their styling. That does not make the result accessible out of the box, but it may make accessibility easier to provide.



