Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Javascript or CSS

It seems that Javascript does some of the same things that CSS does such as manipulating color, backgrounds...When do I use one over the other?

2 Answers

Dave Berning
Dave Berning
17,365 Points

As a general rule, always use CCS first. The animations within CSS are much more efficient on power and much faster than those of JavaScript. It also takes less resources to load and perform CSS than it does JS. CSS goes hand-in-hand with HTML and styles the page; JavaScript is how the page behaves.

If you can't achieve what you need in CSS, then and ONLY then use JS. It's simpler to read and maintain when things like <a> hover states, colors etc are coded in CSS.

I pretty much use JS for backwards compatibility between browsers, images sliders, galleries etc. I never use it for structure and basic animations. CSS3 adopts new features pretty frequently.

One last thing, you don't have to worry too much about this now but it's still a good to be aware of this. Users have the option to disable any JavaScript from loading from any page. If all the functionally and aesthetics is coded in JS, you just lost all of that hard work. It was a big deal in the 90's and early 2000's but not so much now.

Thank you for the complete answer!!!!

Javascript can really help with movement, animations, more advanced web-oriented things etc. Comparatively CSS is really more for standard styling. But it shouldn't be Javascript OR CSS, combine them both and you will get some beautiful results.

Dave Berning
Dave Berning
17,365 Points

I agree that when combined, you can get really nice results. However, since CSS is more of a standard always use CSS for basic animations, color changes, hover states, etc. It's more efficient on power and response times than JS.

If you can do what you want in both CSS and JS...use CSS.

Thank you!