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

HTML

Wenqian Guo
Wenqian Guo
2,612 Points

class, id attributes

Hey guys, I'm not sure why we have class and id attributes...are they just for css styling? and the video says we can only have one id attribute..so why do we even have id attributes? why not use class attributes for everything?

5 Answers

ID stands for identifier, it is meant to be used as a unique identifier for tags. Each tag can only have at most 1 ID, and unlike class, the ID should not be used for other tags. When selecting an ID with Javascript, the default behavior is find one and return, which means the ID itself is assumed to be unique, i.e. if you find one, you would expect none others exist. A common usage of ID beyond CSS is in Javascript or general HTML processing, where you need to uniquely identify an element in your HTML DOM tree, then ID would be your choice since a well-formed HTML assumes ID-uniqueness.

The following example shows what are invalid usages of IDs:

<div id="div1 div2">Can't have multiple IDs! Just like you shouldn't be carrying two driver's licenses.</div>
<div id="div1">Can't have duplicated IDs! Just like your DL number wouldn't be assigned to someone else.</div>

To summarize:

  • Class: can be used with multiple tags; combination allowed; ambiguous broadcasted selection.
  • ID: must be unique; targeted selection.
Wenqian Guo
Wenqian Guo
2,612 Points

is class just for CSS not Javascript? and when we use class with multiple tags is that for conveniently styling a bunch of sections in CSS? I haven't started the CSS videos yet, so I'm not too sure on how that works.

Is class just for CSS not Javascript?

  • A decade ago, yes. But now, no, you can use Javascript to pick out tags with a specific class as well using document.getElementsByClassName(), but note the plural in ...Elements.... It means the selection is ambiguous and it will return a whole bunch of tags as long as they have the class you requested. On the contrary, when you use document.getElementById(), you know which tag you will get as long as the tag you want has the ID you requested.

When we use class with multiple tags is that for conveniently styling a bunch of sections in CSS?

  • YES!

I haven't started the CSS videos yet, so I'm not too sure on how that works.

  • Watch them! You should be able to understand classes better afterwards.
Wenqian Guo
Wenqian Guo
2,612 Points

OK...Thank you! That helped a lot! :)

James Barnett
James Barnett
39,199 Points

> why do we even have id attributes? why not use class attributes for everything?

Using classes for all styling is a pretty good idea.

IDs are needed for anchors in on page links and they can be useful for JavaScript.