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!

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 CSS Selectors Selectors - Beyond the Basics DRY CSS

What is the point of this?

Sorry guys, but this is bothering me. After watching a handful of videos and doing some attribute selectors, Guil switches back to class selectors, working backwards to give some neat styling to a few buttons. Again, I don't see the value in creating a class selector then going back to your HTML when you can target everything in the CSS with a few attribute selectors. It all seems a bit pointless to me as far as DRY goes.

2 Answers

Jamie Moore
Jamie Moore
3,997 Points

Attribute and class selectors are both great for different reasons. Using class selectors lets you apply styles to anything with that class. Using attribute selectors is very specific.

If you're styling buttons across a whole website, you might want all buttons to look the same regardless of their actual element. For example, your a tags, your submit form buttons, and any other button tags. Using class selector makes it easier to apply those styles to each of the different use cases, whereas an attribute selector would need to be applied to any element. In most use cases, you'll use a class selector because they can be re-used over and over again.

I hope that made some sense.

Joshua Graber
Joshua Graber
11,785 Points

I'm not sure if I understand your question exactly, but I think the point is that using attribute selectors in combination with class selectors—and their respective combinations, descendant selectors, and pseudo-elements / -selectors—allows you a lot of flexibility to design your code how it works best for you. If you can do it all with attribute selectors, and that's what makes sense to you, then do it that way. But from my (admittedly limited) experience, classes can be especially helpful when targeting multiple elements, or if you are creating elements dynamically (i.e. inserting them into the DOM via JavaScript).