When to use CSS vs. JavaScript

This is a great counter balance for all those tutorials out there that tell you that you can build feature/interaction XYZ by using CSS instead of JavaScript.  I’m all for using less JavaScript and I certainly think that in the past 18 months the role of JavaScript has taken things a little too far by requiring it just to render text to a page, however, let’s not go so far in the other direction and be anti-JavaScript either.

Use CSS where it makes sense and has the best results, and use JS in the same places where it makes the most sense and has the best results.

For things like animations, it more easily hooks into the browser’s refresh rate cycle to provide silky smooth animations (this can be done in JS, too, but CSS just makes it so damn easy).

And it fails gracefully.

A JavaScript error can bring all of the JS on a page to screeching halt. Mistype a CSS property or miss a semicolon? The browser just skips the property and moves on. Use an unsupported feature? Same thing.

An excerpt from When to use CSS vs. JavaScript

View original article

Subscribe to our Newsletter

Add your email address and receive an email every Friday covering off everything worth knowing about building your websites responsively.