Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS CSS Selectors Selectors - Beyond the Basics Attribute Selectors

Peter Huang
Peter Huang
3,590 Points

Why do we use class="form-contact" instead of just simple class of " .form-contact " ?

Why do we use class="form-contact" instead of just simple class of " .form-contact " ?

Is this something you MUST use on forms for it to work and regular class will not work? I dont understand this

1 Answer

Steven Parker
Steven Parker
218,596 Points

It sounds like you stopped your video early, or just missed the part at about 5:26 where the instructor says:

Well, now you may be asking, why target elements with attribute selectors instead of the classes or ids we learned about earlier. Well, most of the time there's no real advantage to using these attribute selectors when it comes to targeting ids and class attributes. It's usually best to use a class or id selector, in this case.

He then makes those changes to the stylesheet, and then goes on to show a more practical example of using an attribute selector to look for an element with a particular type.

Peter Huang
Peter Huang
3,590 Points

Thank you for this. Yes I've stopped video early to ask which is a bad habbit of mine. I just got another question though, is this method only good to target specific form bar type area and change color? he goes on to teach these but I don't see the difference in still using the regular class or ID to target these

Steven Parker
Steven Parker
218,596 Points

An ID can only target one element, so it's not useful when more than one is involved. And using the type attribute to target elements would be advantageous when they don't all share the same class (or if there are other elements with the same class that you don't want included).