"Error Handling in Swift" was retired on May 31, 2020.

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 Selectors Selectors - Beyond the Basics Styling Form Buttons and Links with Attribute Selectors

I still don't understand why we can't just use ids and classes? It seems to have the exact same effect...

In fact when I used the attribute selectors CSS it didn't have any effect and only had the intended effect after I used a class instead

3 Answers

Joel Bardsley
Joel Bardsley
31,249 Points

That's true Peter, but there may be times where the html markup is generated by a third-party plugin or CMS and you wouldn't be able to add your own IDs or classes. Knowing these selectors is useful for us to be able to apply styles to specific elements under those circumstances.

Ah okay this makes a lot of sense. Thanks very much!

Kevin Korte
Kevin Korte
28,149 Points

Well, the lesson is called Selectors - Beyond the Basics, which is why they would have used attribute selectors instead here. You can us a class or id, but that would defeat the purpose of the course.

There are some situations in the wild you'll run into as a developer where an attribute selector makes sense. Usually not, but it's good to have such knowledge in your toolbox, when the situation arises.

Sometimes you may be working within an ecosystem where you can't change the html markup, or its incredibly difficult to, as so an attribute selector can be a great option. Other times it may be challenging, if not impossible to predict all the elements you want to target with a class, and again, the attribute selector can be a good option.

I know in the lessons it seem so easy to add a class, why can't we add a class. But for simplicity, the lessons have boiled away all of the other complexities that will exist when you're coding in the wild, thus the reason for attribute selectors.

And attribute selectors do work, they're just quite picky, so if didn't work for you before, the selector you were trying to use was incorrect and did not match the element you were intending to target.

Okay, thanks. I was just unsure as to when and why I would not be able to use classes and ids as it seemed easier. This was really helpful, thanks!

Kevin Korte
Kevin Korte
28,149 Points

You bet. There will be times you just can't (at least easily) change the html markup that you get to style, and so attribute selectors can play an important role.

Sergi Oca
Sergi Oca
7,981 Points

Thank you for that insight, I believe it it should have been mentioned in the course even if it is a bit apparent that is for the course purposes, but yea when you are starting sometimes what is best practice or not can get a bit blurry.