Hacker News new | past | comments | ask | show | jobs | submit login
Solving FizzBuzz with CSS (dabblet.com)
123 points by jackiebalzer on May 7, 2012 | hide | past | favorite | 27 comments



Correct me if I'm wrong, but I don't believe you need any of the :not statements, and instead or (3n) and (5n) you can just use 15n for the 'fizzbuzz'.

The cascade of the CSS will take care of the cases the div is a 'fizzbuzz'.

Sorry I can't post the example, Dabblet is working strangely for me this morning.

CSS that should work:

  /**
   * FizzBuzz with CSS
   */

  body {
  	counter-reset: fizzbuzz;
  }

  div {
  	width: 100px;
  	height: 20px;
  	background: #ddd;
  	margin: 0 0 10px;
  }

  div::after {
  	content: counter(fizzbuzz);
  	counter-increment: fizzbuzz;
  }

  div:nth-child(3n)::after {
  	content: "fizz";
  }  

  div:nth-child(5n)::after {
  	content: "buzz";
  }

  div:nth-child(15n)::after {
  	content: "fizzbuzz";
  }


This should also work:

div:nth-child(3n):after { content: ""; }

div:nth-child(3n):before { content: "fizz"; }

div:nth-child(5n):after { content: "buzz"; }


I retract my answer in favor of this. Clever.


I think your answer is a lot more readable, though. I have to squint at jontro's version for a while before determining that it'll actually product the string "fizzbuzz".


Brilliant.


Correct, you don't need the :nots as long as the CSS is in that order, since the :after content would be overridden correctly. You would need them if you changed the order of the statements.

I started the experiment using background color instead of "fizz", border color instead of "buzz", and box-shadow instead of "fizzbuzz" which necessitated the need for :nots since the properties weren't overriding.


Heh... how this looks in IE 8: http://i.stack.imgur.com/Fz3jT.png


To be fair, isn't that what most sites look like in IE8?


hahah win.


To be fair, IE8 was released on March 2009 (according to wiki), Chrome at that time was v1.0 and Firefox was v3.0.


Usually IE8 users are stuck in IE8 unless they let the OS upgrade their browser, so most of the users that started with IE8 are still stuck in 2009. Chrome/FF users tend to have less issues with upgrades.


Not to mention those of us in a workplace still using XP (and thus unable to upgrade past IE8).


All horrible arguments. "Stuck?" Download a real browser.

IE8 viewers expect devs to slow progress or cater to them would be like horse and buggy drivers from days of yore lobbying to prevent cars from going faster than horses.

Stop complaining, get off your horse and get a (free) car. Encourage your clients to do likewise.


Corporate environments sometimes lock down their computers completely, and don't give admin/install privileges, let alone OS updates.

So yes, "stuck".


The dev community should not be at the mercy of incompetent sysadmins. Evolve or die. Such is life. Such is business.


good thing you don't need to install chrome to run it then!


No rest until CSS is Turing complete.


You mean like this CSS-implementation of Rule 110? http://eli.fox-epste.in/rule110-full.html


For those who tried this in Chrome, like me, it won't work :(

Try it in Firefox instead.


Chrome didn't work for me either, firefox was fine


Worked fine for me.


ditto


Goes to show how many (greatly needed) programmatic features CSS is adding. Also shows that some people love a creative way to attack a problem.


As God is my witness, CSS will need to be split into a presentation language and a logic language. /s


You know, that’s not such an outlandish claim. CSS is accumulating features that seem to me completely out of place in a styling language. Then again, HTML isn’t perfect, nor JavaScript, nor PHP or any language—but we still get stuff done with them, so who am I to complain?


Goes to show what a hack-riddled mess Web presentation still is.


Is this some regional drinking game? Like Three-Man?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: