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

CSS jQuery Basics (2014) Creating a Simple Lightbox Perform: Part 3

Felix Sonnenholzer
Felix Sonnenholzer
14,654 Points

Why not add CSS using JQuery?

Hello,

I was wondering, why we wouldn't add the CSS using JQuery as well, like we did with the HTML.

It was said earlier, that we don't want the user to download anything unnecessary. But adding CSS into the style sheet isn't that exactly what we are doing? Or is it, that when adding CSS using JQuery it could mess it up? Wouldn't make any sense though.

Looking forward to your answers

1 Answer

Arturo Alviar
Arturo Alviar
15,739 Points

A stylesheet is necessary since relying on jQuery to handle your CSS rules is messy and not all users have JavaScript enabled. Generally, when using jQuery for DOM manipulation, you append classes to elements that are already defined in the stylesheet. Using such a technique allows you to have organized and readable code.

Felix Sonnenholzer
Felix Sonnenholzer
14,654 Points

Thank you for your answer. Maybe I phrased it bad, but I didn't mean to append the whole CSS, but only adjust the CSS of the HTML we added through JQuery. In this project it would be the CSS of #overlay. Because if JavaScript was disabled it would the overlay wouldn't appear and thus the related CSS wouldn't appear either.

I understand what you mean with making organized and readable code when we append classes to elements. I was just wondering, because Andrew said something about optimizing things, and I thought that would be related.

Anyhow thank you for your reply