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 jQuery Basics (2014) Creating a Spoiler Revealer Perform: Part 3

Why not use CSS? What are the advantages of using jQuery for simple effects (like showing and hiding content) over CSS?

Revealing spoilers is a core feature of this project, so it seems risky to use jQuery when JS might be disabled in the browser. I've re-created the same effect (with minor layout differences) in CSS, using the display property. I wonder, though, which would be better from a performance perspective: CSS or jQuery?

Here's a snapshot of my workspace: https://w.trhou.se/dfu8ofmy2z

Would love to hear your thoughts!

2 Answers

Steven Parker
Steven Parker
229,644 Points

It makes sense for the course to use an easy task like this to introduce the use of jQuery. But I agree that in actual practice if your objective can be done purely in CSS, and you have no other need for JavaScript, then implementing with pure CSS is probably the best option. Besides avoiding any browser restriction issues, CSS will generally result in better performance as well.

You can actually reproduce the functionality of the jQuery project more exactly in CSS with slightly different coding.

Jan Svensson
Jan Svensson
10,005 Points

I would say that for performance it doesn't matter in this case because it is such a simple feature. The computer is going to be very fast in both cases. I would probably choose the method that is the most intuitive and makes more sense.

CSS is good for styling, and even though you can use it for more advance feature stuff, it's usually good to leave that to JS. In a big project CSS will be too limiting and JS will be much easier.

For a small feature like this it can make sense to do it in CSS aswell.