Demo

Sieve of Eratosthenes in HTML and CSS

An ancient method for finding prime numbers, by crossing out the multiples of each prime in turn.

What it is

Around 2,200 years ago, the Greek mathematician Eratosthenes described a way to find prime numbers without dividing anything. Write out the numbers; take the first prime, 2, and cross out all its multiples; move to the next number not crossed out, which is the next prime, and cross out its multiples; and carry on. Whatever is left is prime.

Below, CSS has done the crossing out: each colour marks the multiples of one prime, and the numbers left in plain type are the primes from 2 to 113. Tick the box to hide everything that was crossed out.

Working example

  • Multiple of 2
  • Multiple of 3
  • Multiple of 5
  • Multiple of 7

How it works

The HTML is a checkbox and an ordered list of 120 empty list items. As in FizzBuzz, CSS numbers the items with a counter and finds multiples with :nth-child(). The sieve adds two things: an exception for the primes themselves, and a checkbox that changes what's shown.

The HTML
<input type="checkbox" id="primes-only">

<ol class="numbers">
    <li></li>
    <li></li>
    <li></li>
    <!-- … and so on, up to 120 -->
</ol>

Numbering

The counter goes up by one on every item, and each item prints it. The first item is 1, which is neither prime nor a multiple of one, so it's made invisible rather than removed: it still takes up its place, so every other item's position in the list stays the same as its number.

Numbering source
.numbers { counter-reset: number; }
.numbers li { counter-increment: number; }
.numbers li::before { content: counter(number); }

.numbers li:first-child { visibility: hidden; }

Crossing out

Each prime, P, has one rule. :nth-child(Pn) matches every multiple of it, and :not(:nth-child(P)) leaves out the prime itself. So :nth-child(2n):not(:nth-child(2)) crosses out 4, 6, 8 and so on, but not 2. Whatever no rule matches is prime.

A number with more than one prime factor, such as 30, is matched by several rules. The rules are in the order 2, 3, 5 and 7, and the last one to match wins, so 30 takes the colour for 5.

Crossing out source
.numbers li:nth-child(2n):not(:nth-child(2)) {
    background: var(--brand-amber);
}

.numbers li:nth-child(3n):not(:nth-child(3)) {
    background: var(--brand-rose);
}

.numbers li:nth-child(5n):not(:nth-child(5)) {
    background: var(--brand-blue);
}

.numbers li:nth-child(7n):not(:nth-child(7)) {
    background: var(--brand-green);
}

Showing only the primes

The checkbox is the only thing the sieve remembers. :has() lets the sieve as a whole see whether it's ticked, and if it is, everything that was crossed out is hidden. The tests are the same ones again: a number that was crossed out by a rule is hidden by the matching rule.

Showing only the primes source
.sieve:has(#primes-only:checked) .numbers li:nth-child(2n):not(:nth-child(2)),
.sieve:has(#primes-only:checked) .numbers li:nth-child(3n):not(:nth-child(3)),
.sieve:has(#primes-only:checked) .numbers li:nth-child(5n):not(:nth-child(5)),
.sieve:has(#primes-only:checked) .numbers li:nth-child(7n):not(:nth-child(7)) {
    visibility: hidden;
}

Why four primes are enough

Every number up to 120 that isn't prime has a prime factor no bigger than its square root, and the square root of 120 is just under 11. So crossing out the multiples of 2, 3, 5 and 7 catches all of them. A longer list needs more primes: to go to 1,000, you'd add rules for 11 up to 31.

The end result

Every number that a smaller prime divides is coloured in, and the numbers left in plain type are the primes from 2 to 113. Ticking the box leaves just those on show.

Why it matters

This is a real algorithm, more than two thousand years old, carried out from start to finish by rules that only describe what each number should look like. But there's a catch. In a program, the sieve finds each next prime itself and carries on. Here, someone had to know which primes to write rules for. The CSS carries out the algorithm, but it can't extend itself: it computes within limits a person set in advance.