Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is such a great example of how CSS is often overlooked. Very impressive. I learned a lot from reading it.

I bet if you had asked how to do this on StackOverflow without any prompting about language, you would have seen a LOT of javascript Me included.

EDIT: I literally just went to a project I'm working on and removed a piece of JS code that animated a feature because of what I learned from this example.



It's incredible how much you can achieve with using web platform features directly and using the minimal amount of JS possible. Pseudo elements, draggable="true", CSS animations... the list goes on and on.

Of course, the real trouble starts when you need to support older browsers and you realize you need to polyfill or change your strategy.


What is the advantage of doing something in CSS vs JS?


All of the peer answers are excellent, here are my $0.02:

As a design principle, it makes sense to use/re-use the capabilities of a component, rather than an agent acting on that primitive. For example, HTML already has a built-in CSS engine that can animate. Re-writing the animation in JavaScript is redundant and adds more code, which means more bugs, more size, more maintenance, etc.

Consider this example in C: using printf("%f", val) versus writing your own convert_float_to_characters_to_stdout(val) function.

There are cases where you may want to do the latter because printf is a very large function for embedded controllers and sometimes excludes format specifiers to save space, especially %f. But normally you just use what you are given rather than re-inventing the wheel.


If it is done in JS it is going to be done on the CPU. Probably on the main thread even which could make it choppy.

If it is done with CSS it is much more likely it'll be done by the GPU and will not interrupt the main thread and cause 'jank'.


For one thing, it can continue to work when JS is not working for whatever reasons.


It's an application of the Rule of Least Power.


I would expect it to be faster (use less computing resources) as its handled directly by the browser and not via an JavaScript interpreter.




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

Search: