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 How to Make a Website CSS: Cascading Style Sheets Style the Basic Elements

Jonathan Baumgarten
PLUS
Jonathan Baumgarten
Courses Plus Student 1,467 Points

Why the need for the "id" selecter?

The "id" selecter seems to be used to make it easier to know what is being selected in the html document in our css docmunet. But why not simply(in our css file) say the element explicitly, such as "header" or "body",, why the need for a creation of an "id" selecter to specify an element?

4 Answers

David Curtis
David Curtis
11,301 Points

Hi Yoni,

I don't know which problem in the course you are referring to, but the ID is used to give a unique identifier to an element so that you can grab that exact one for manipulation or styling (CSS or JavaScript). For instance, if you just used "p", you would grab all paragraph tags on that page; however, if you give the paragraph an ID, you can grab the specific paragraph and not just all of them. Even if you give a paragraph an ID, you can still use the p selector if you want to style it and all paragraphs (like making the font of all paragraphs sans-serif, for example)

I hope that answers your question!

David

Neil Northrop
Neil Northrop
5,095 Points

Hey Yoni,

Great question! The "ID" selector has an awesome use to specify a specific element. Yeah, when you consider the header or body, the "ID" selector doesn't really seem useful.

Now imagine you have 10, 20, or 30 different <img> or <a href> tags on a page. How would you specifically pick one over another? When you start getting multiple tags that are the same, using the "ID" selector helps out a lot!

I hope that helps you!