Demo
Noughts and crosses in HTML and CSS
A two-player game that takes turns, refuses illegal moves and spots a win or a draw.
What it is
Two players, one screen: X goes first, then take turns. The game refuses a square that's already taken, says whose turn it is, and spots a line of three or a draw. "New game" clears the board.
Working example
How it works
The game's memory is nine groups of radio buttons, one group for each move, with an option in each group for every square. Radio buttons suit a game well: a group can only have one choice, so a move can only go in one square. Each option is hidden inside a label, and the labels are laid over the board.
The HTML
<label class="cell move-3 square-4">
<input type="radio" name="m3" value="4" class="x">
Put X in the centre
</label>
Taking turns
The options for a move appear once the move before it has been made, and disappear once it has been made itself, so only one move is ever open. Odd moves belong to X and even moves to O, so the board never has to count whose turn it is. It only has to know which move comes next.
Taking turns source
.game:has([name="m2"]:checked):not(:has([name="m3"]:checked)) .move-3 {
display: grid;
}
Keeping squares closed
Once a square has been played, whichever move played it, it's hidden from every later one. Hiding a label hides the radio button inside it too, so a taken square can't be chosen with the keyboard either. The rule leaves out the label that was chosen, because that's the one showing the mark.
Keeping squares closed source
.game:has([value="4"]:checked) .square-4:not(:has(:checked)) {
display: none !important;
}
Saying whose turn it is
The message above the board is empty in the HTML, and CSS writes it. There's a rule for each move, saying who plays next. They come in order, so as the game goes on, each one takes over from the last.
It's also where having no JavaScript costs something. A script could ask a screen reader to announce each turn. Text that CSS writes can be read, but it may not be announced when it changes, so a screen reader user may need to go back to the message after each move.
Saying whose turn it is source
.game:has([name="m1"]:checked) .status::before {
content: "O to play";
}
Spotting a win
There are eight lines of three, and each has its own rules for X and for O. A rule looks for three of the same mark by testing for three checked options at once. When it matches, it announces the winner, hides every square that hasn't been played, and marks the line. These are the rules for X on the diagonal from top left.
Spotting a win source
.game:has(.x[value="0"]:checked):has(.x[value="4"]:checked):has(.x[value="8"]:checked) .status::before {
content: "X wins!";
}
.game:has(.x[value="0"]:checked):has(.x[value="4"]:checked):has(.x[value="8"]:checked) .cell:not(:has(:checked)) {
display: none !important;
}
.game:has(.x[value="0"]:checked):has(.x[value="4"]:checked):has(.x[value="8"]:checked) .square-0, .game:has(.x[value="0"]:checked):has(.x[value="4"]:checked):has(.x[value="8"]:checked) .square-4, .game:has(.x[value="0"]:checked):has(.x[value="4"]:checked):has(.x[value="8"]:checked) .square-8 {
--winning: 1;
}
Spotting a draw
If the ninth move has been made, the board is full. If that move also completes a line, the win is announced instead: its rule tests for three things rather than one, which makes it more specific, so it takes precedence.
Spotting a draw source
.game:has([name="m9"]:checked) .status::before {
content: "It's a draw.";
}
Starting again
The "New game" button is HTML. A reset button puts every input in its form back the way it started, which unticks every move, and the board goes back to the beginning.
Starting again source
<button class="button" type="reset">New game</button>
Written by a program
In all, it takes 75 rules, written by a small program when the site is built rather than by hand. Every one is a plain CSS rule. The program just saves typing out the same pattern for every move, square and line, and tests check that all sixteen winning lines are there.
The end result
Two players can play a whole game. Every move is legal, every win and draw is spotted, and it ends when it should.
Why it matters
A game has to remember what's happened and decide what's allowed next, which is the third of the four tests. HTML on its own can't decide, and CSS on its own, as most people write it, can't remember. Together they run a whole game, rules and all.
What it doesn't do is play against you. A computer opponent has to look ahead, imagining moves and their replies, and choose the best: a search, which rules like these don't do. They can referee a game, not play one.
There are two ways round that, and neither is simple. Noughts and crosses is small enough that a program could work out the best reply to every possible position in advance and write each one out as a CSS rule: a lookup table, which plays perfectly without thinking at all. Or, with the newest CSS, an emulated processor like x86CSS could run a game-playing program for real. Both show the same thing: in CSS, playing a game is possible, but it takes far more than refereeing one.