zlacker

Fizz Buzz in CSS

submitted by froobe+(OP) on 2025-12-05 20:18:22 | 104 points 22 comments
[view article] [source] [links] [go to bottom]
replies(8): >>kevins+re >>susam+pf >>carl_d+kg >>graiz+ev >>dsmmck+lz >>geneze+rA >>Metabo+LA >>smrq+543
1. kevins+re[view] [source] 2025-12-05 21:30:20
>>froobe+(OP)
I love this, it's a very clever and funny way to solve the problem. Makes me think about how there are infinite routes from A to B, some more scenic and whimsical than others.. as well as all the people I've met along the way who would be so pissed and pedantic about how this isn't a "real solution" LOL
replies(1): >>hyperh+vn
2. susam+pf[view] [source] 2025-12-05 21:33:57
>>froobe+(OP)
A shorter solution is possible with an ordered list (<ol>) if we're willing to ignore the untidy output:

  li:nth-child(3n), li:nth-child(5n) { list-style: none }
  li:nth-child(3n)::before { content: "Fizz" }
  li:nth-child(5n)::after { content: "Buzz" }
Example: https://susam.net/code/web/css-fizz-buzz-ol.html

  $ curl -sS https://susam.net/code/web/css-fizz-buzz-ol.html | sed -n '/none/,/after/p' |  tr -d '[:space:]' 
  li:nth-child(3n),li:nth-child(5n){list-style:none}li:nth-child(3n)::before{content:"Fizz"}li:nth-child(5n)::after{content:"Buzz"}
  $ curl -sS https://susam.net/code/web/css-fizz-buzz-ol.html | sed -n '/none/,/after/p' |  tr -d '[:space:]' | wc -c
  129
But I don't quite like how misaligned the numbers and the words look in this version. Correcting that would call for extra code that would cancel out the characters saved.
replies(1): >>clucki+nj
3. carl_d+kg[view] [source] 2025-12-05 21:37:55
>>froobe+(OP)
Ignoring the size of the HTML in addition to the CSS, it’s fun, but not really fair when talking about code golf. Beyond a few numbers, you need to include some JavaScript and generating a million list elements. But those bytes count …
◧◩
4. clucki+nj[view] [source] [discussion] 2025-12-05 21:56:38
>>susam+pf

    list-style-position: inside;
replies(1): >>susam+4l
◧◩◪
5. susam+4l[view] [source] [discussion] 2025-12-05 22:06:51
>>clucki+nj
Yes! However, like I mentioned in my previous comment, corrections like this cancel out the number of bytes saved with the <ol>-based solution.

I mean, the solution in the original post is 152 characters long.

The <ol> based solution is 129 characters long. Shorter but uglier.

If we add your correction, we get neater output, which is nice, but it comes at the cost of 30 additional characters in the minified code thereby making the solution 159 characters long.

  li { list-style-position: inside }
  li:nth-child(3n), li:nth-child(5n) { list-style: none }
  li:nth-child(3n)::before { content: "Fizz" }
  li:nth-child(5n)::after { content: "Buzz" }
◧◩
6. hyperh+vn[view] [source] [discussion] 2025-12-05 22:23:04
>>kevins+re
The problem is that you have to define the problem enough to avoid the fact that it's trivial to output the string "1,2,Fizz,4,Buzz,......" and fulfill the assignment. You can, in fact, output "$1,$2,Fizz,$4,Buzz,..." where $ is any prefix itself divisible by 15 (there are other templates for the other situations but it clearly does repeat endlessly.)
7. graiz+ev[view] [source] 2025-12-05 23:12:41
>>froobe+(OP)
145 using P instead of li.

<style> p{counter-increment:n} p:not(:nth-child(5n)):before{content:counter(n)} p:nth-child(3n):before{content:"Fizz"} p:nth-child(5n):after{content:"Buzz"} </style><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p>

replies(1): >>rebane+OA
8. dsmmck+lz[view] [source] 2025-12-05 23:42:08
>>froobe+(OP)
144, using css variables, with fallback and p instead of li

https://codepen.io/dsmmcken/pen/WbwYOEQ?editors=0100

p{counter-increment:n;--n:counter(n)}p:nth-child(3n){--f:"Fizz"}p:nth-child(5n){--b:"Buzz";--n:''}p::after{content:var(--f,var(--n))var(--b,'')}

9. geneze+rA[view] [source] 2025-12-05 23:51:24
>>froobe+(OP)
To nitpick a bit, I'd say there is something that is "placed outside the stylesheet".

Now, arguably it's not a "number or word that appears in the output", that's true, but it is a part of the logic of FizzBuzz. It's the fact that there are 100 and exactly 100 <li> elements in the HTML part.

I mean, it wouldn't be a correct FizzBuzz without precisely that HTML. Having exactly 100 <li> elements is implementing the part of the logic that a. loops, and b. stops at 100.

But of course this is just nitpicking, as I said ;)

replies(1): >>culi+BB
10. Metabo+LA[view] [source] 2025-12-05 23:54:01
>>froobe+(OP)
104 :nth-child(3n){list-style:"Fizz"}:nth-child(5n){list-style:"Buzz"}:nth-child(15n){list-style:"FizzBuzz"}

data:text/html,<style>:nth-child(3n){list-style:"Fizz"}:nth-child(5n){list-style:"Buzz"}:nth-child(15n){list-style:"FizzBuzz"}</style><ol id=o><script>o.innerHTML='<li>'.repeat(99)</script>

replies(2): >>Metabo+pE >>cefqrn+QG
◧◩
11. rebane+OA[view] [source] [discussion] 2025-12-05 23:54:44
>>graiz+ev
im not sure how you counted that as 145, but here's 137 for the css

<style>p{--n:counter(n);counter-increment:n;&:before{content:var(--n)};&:nth-child(5n){--n:"";&:after{content:"Buzz"}}&:nth-child(3n){--n:"Fizz"</style><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p><p>

◧◩
12. culi+BB[view] [source] [discussion] 2025-12-06 00:00:19
>>geneze+rA
HTML is just the runtime of CSS
◧◩
13. Metabo+pE[view] [source] [discussion] 2025-12-06 00:23:57
>>Metabo+LA
103 :nth-child(3n){list-style:"Fizz"}:nth-child(5n){list-style:"Buzz"}:nth-child(15n){list-style:"FizzBuzz"
replies(1): >>Metabo+yE
◧◩◪
14. Metabo+yE[view] [source] [discussion] 2025-12-06 00:24:46
>>Metabo+pE
98 :nth-child(5n){list-style:""}:nth-child(3n){list-style:"Fizz"}:nth-child(5n)::after{content:"Buzz"
replies(1): >>Metabo+EG
◧◩◪◨
15. Metabo+EG[view] [source] [discussion] 2025-12-06 00:40:51
>>Metabo+yE
95 :nth-child(5n){list-style:}:nth-child(3n){list-style:"Fizz"}:nth-child(5n)::after{content:"Buzz
replies(1): >>Metabo+WG
◧◩
16. cefqrn+QG[view] [source] [discussion] 2025-12-06 00:43:19
>>Metabo+LA
89: :nth-child(3n)::marker{content:"Fizz"var(--b,)}:nth-child(5n){list-style:"Buzz";--b:"Buzz

with help from @bulmenisaurus

◧◩◪◨⬒
17. Metabo+WG[view] [source] [discussion] 2025-12-06 00:44:12
>>Metabo+EG
68 :nth-child(3n){list-style:"Fizz"}:nth-child(5n)::after{content:"Buzz
replies(2): >>eurlei+WI >>bulmen+gd1
◧◩◪◨⬒⬓
18. eurlei+WI[view] [source] [discussion] 2025-12-06 01:06:01
>>Metabo+WG
67 :nth-child(3n){list-style:"Fizz"}:nth-child(5n):after{content:"Buzz
replies(1): >>Metabo+0X
◧◩◪◨⬒⬓⬔
19. Metabo+0X[view] [source] [discussion] 2025-12-06 03:26:52
>>eurlei+WI
thank you sir! i can improve the html op css was 152, we have the whole page at 144

data:text/html,<ol id=o><script>o.innerHTML='<li>'.repeat(100)</script><style>:nth-child(3n){list-style:"Fizz"}:nth-child(5n):after{content:"Buzz

replies(1): >>throwa+YY
◧◩◪◨⬒⬓⬔⧯
20. throwa+YY[view] [source] [discussion] 2025-12-06 03:47:25
>>Metabo+0X
Your code is showing the numbers 5, 10, 20. that's not correct, is it?

Yours prints 1. 2. Fizz 4. 5. Buzz Fizz 7.

But it should be 1. 2. Fizz 4. Buzz Fizz 7.

◧◩◪◨⬒⬓
21. bulmen+gd1[view] [source] [discussion] 2025-12-06 07:17:33
>>Metabo+WG
This version fails for numbers like 5, showing "5. Buzz". In the case of a number divisible by 5 but not 3 nothing prevents the 5 in the ::marker from rendering.

Edit: it looks like your version can be fixed into a working 85:

  :nth-child(5n){list-style:'';&:after{content:"Buzz"}}:nth-child(3n){list-style:"Fizz"
22. smrq+543[view] [source] 2025-12-07 01:52:55
>>froobe+(OP)
Am I the only one here who's never seen CSS counters before? Interesting feature, what are they useful for practically?
[go to top]