Demo
FizzBuzz in HTML and CSS
The classic programming interview question: count to 100, saying "fizz" for multiples of 3 and "buzz" for multiples of 5.
What it is
FizzBuzz began as a counting game for children. Players count upwards in turn, saying "fizz" instead of any number divisible by 3, "buzz" instead of any number divisible by 5, and "fizzbuzz" for numbers divisible by both. It became a famous first test in programming interviews, because solving it takes a loop, a counter and a couple of decisions: the basics of programming in miniature.
The list below is a hundred empty list items. Everything you can read in it was put there by CSS.
Working example
How it works
The HTML is an ordered list, <ol>, of a hundred list items, <li>, with
nothing in any of them. The CSS has to work out each item's number, test what that number is divisible by, and
write the answer. It does it without ever dividing, using three features: a counter, the
:nth-child() selector, and generated content.
The HTML
<ol>
<li></li>
<li></li>
<li></li>
<!-- … and so on, up to 100 -->
</ol>
Counting
A CSS counter is a number the browser keeps while it reads through the page. The list sets one called
number to 0, and every item adds 1 to it, so the first item's number is 1, the second's is 2, and so
on. The HTML never says which item is which.
Counting source
ol { counter-reset: number; }
li { counter-increment: number; }
Testing for divisibility
:nth-child(3n) matches every third child of its parent. The n stands for every whole
number in turn, so 3n is 3, 6, 9, 12 and so on: exactly the numbers divisible by 3. Matching it is
the same as asking whether the item's number leaves no remainder when divided by 3, which most programming
languages write as number % 3 == 0. :nth-child(5n) asks the same about 5.
Plain numbers
Items divisible by neither should show their number. :not() turns a test round, so
:not(:nth-child(3n)) matches items that aren't divisible by 3. Writing two of them together means
both must hold: not divisible by 3, and not divisible by 5. Those items get an ::after, a piece of
content that CSS adds at the end of an element, holding the counter's value.
Plain numbers source
li:not(:nth-child(3n)):not(:nth-child(5n))::after {
content: counter(number);
}
Fizz
Items divisible by 3 get the word "Fizz" in their ::before, the content CSS adds at the start of an
element.
Fizz source
li:nth-child(3n)::before {
content: "Fizz";
}
Buzz
Items divisible by 5 get the word "Buzz" in their ::after. It's free for them to use, because the
plain numbers rule leaves out anything divisible by 5.
Buzz source
li:nth-child(5n)::after {
content: "Buzz";
}
FizzBuzz
There's no rule for FizzBuzz, because it doesn't need one. A number divisible by both 3 and 5, such as 15, 30 or
45, matches the Fizz rule and the Buzz rule, so it gets both. Fizz is at the start of the element and Buzz is at
the end, with nothing in between, so together they read "FizzBuzz". That's why one uses ::before and
the other ::after: two rules writing to the same place would overwrite each other.
The colours do need a rule each, because an element has only one background. Multiples of 3 are rose and multiples of 5 are blue; a multiple of 15 is both, so a last rule, which wins because it comes last, makes it violet.
Colours source
li:nth-child(3n) { background: var(--brand-rose); }
li:nth-child(5n) { background: var(--brand-blue); }
li:nth-child(15n) { background: var(--brand-violet); }
The end result
Every item divisible by neither 3 nor 5 shows its number. Every item divisible by 3 starts with Fizz, every item divisible by 5 ends with Buzz, and every item divisible by both says FizzBuzz. The list here has a hundred items, but the rules work for a list of any length.
Why it matters
If FizzBuzz is a fair first test of a programmer, it's a fair first test of a language. CSS has everything it
needs: the counter counts, :nth-child() tests for divisibility, and the selectors choose what to
show. There's no loop to write, because the browser already applies every rule to every item.
What the CSS can't do is make the list longer. The rules would carry on for ever, but the HTML decides how far they count.