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 trialNikolay Batrakov
9,604 Pointsclass vs attribute selectors
why do we need to use attribute selectors instead of classes or id selectors?
3 Answers
James Barnett
39,199 PointsThere are lots of great use cases for attribute selectors, style images by file type, diagnostic CSS for missing alt text, icons indicating off page links.
Here's a good article on the topic: http://www.hongkiat.com/blog/css3-attribute-selector/
James Conner
9,821 Points+1
also, more flexibility when targeting specific groups of content on the page where items within that group might not make sense to all use the same class, but might already have similar attributes, especially when introducing wildcards (*) into the attribute selectors.
Nikolay Batrakov
9,604 PointsWell it sounds to me a bit messy in terms of code readability but OK, I got the point, thank you guys!
James Gill
Courses Plus Student 34,936 PointsEfficiency, I think. Also, when using JavaScript/jQuery, I hear it's better to use attribute instead of class selectors, to improve maintainability and speed.
James Conner
9,821 Pointsthat +1 was intended for your answer, I only mean to add to it not take it over. I think I accidentally upvoted my own comment? sorry..
James Conner
9,821 PointsJames Conner
9,821 Pointsglad you mentioned all of this, I don't use them often but really should.