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!

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


Christian Colella
Christian Colella
2,599 Points

Better understanding of id attribute.

We added a id attribute in div and in the header a point. I guess it made sense when you created the division group but it just seemed random putting it in the anchor section.

So can id attributes just go anywhere. He didn't really explain why he put it at the end of the href. Why wouldn't a div be created to select that whole piece and call that div "logo"

I hope I make sense with this and thank you in advance.

5 Answers

I think it's best to think of IDs as names, and classes as nouns. So, a person can be called Joe Smith, and that would be that person's ID. It's unique to him. A chair however, doesn't have an Id, it's a noun, so in this analogy, a chair is a div with class "chair".

Another complication is that you can link to ids in a page, as an anchor. For example, you have a whole page about how much you love dogs, and halfway through it, you have a picture of your own dog. You can click a link in the page that would automatically scroll to that image. Let's say that image had the id of "dog". On the top of this page, there's a link that says "Go to dog picture", with href="#dog". It would now find an element with the ID of dog and scroll to that.

Alexander Smith
Alexander Smith
2,769 Points

You want to use id over class for anything that's only going to be used once on page. ID takes preference over over classes so it's a good way of making sure something important looks well important.

But these days it's mainly used for javascript when you need to identify specific objects.

Oh! Almost forgot that ids can be used for same page links/navigation. Basically if you have a say a section near the bottom of the page with the id footer you can provide a link to the bottom with a <a href="#footer">.

Your question isn't completely clear. But ID's are to be used to target, one unique html element.

You also mentioned an href. So I am assuming you're talking about something like this:

<a href="#comments" id="link-to-comments">Go To Comments</a>

Lets say that link were at the top of the page and then at the bottom of the page (with much scrolling to get there) you had a div like so:

<div id="comments">
 <!-- Bunch of social site style comments -->

Clicking the link would scroll the page down to the element with the ID of comments.

Hope this answers your question.

Hi Christian,

I was really confused by class/id when I first started out. I looked through MDN and found the descriptions to be somewhat confusing there as they didn't show any examples of the code in action.

Take a look at the link below (CSS-Tricks from Treehouse) to see if this helps to explain in more detail. https://css-tricks.com/the-difference-between-id-and-class/

Hopefully that helps with your question.

Christian Colella
Christian Colella
2,599 Points

I appreciate that help response everyone and yea I do not think I was clear with my question. I was pretty exhausted watching videos and practicing.

I was referencing creating an id to then alter color, size, font, etc in css. After a break I cam and played with it in different areas and it made more sense. Just need more practice is all.

Again I appreciate all the help. This is an amazing website.