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 CSS Foundations Selectors Basic Attribute Selectors

Nikolay Batrakov
Nikolay Batrakov
9,604 Points

class vs attribute selectors

why do we need to use attribute selectors instead of classes or id selectors?

3 Answers

James Barnett
James Barnett
39,199 Points

There 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/

glad you mentioned all of this, I don't use them often but really should.

+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
Nikolay Batrakov
9,604 Points

Well it sounds to me a bit messy in terms of code readability but OK, I got the point, thank you guys!

Efficiency, I think. Also, when using JavaScript/jQuery, I hear it's better to use attribute instead of class selectors, to improve maintainability and speed.

that +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..