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

id vs class

When do I use id? When do I use class.

3 Answers

Tristan,

Use IDs when you're describing something unique--for example, a section that appears only once. Use classes when you're describing something you might use again.

An id refers to a specific object in the document object model and is unique to that one object.

A class is re-usable with many objects on a page.

You may have a portfolio of images. The <ul> containing the list of images may have an id of "portfolio-images" because it only exists once on the page. The <li> tags in the list may have a class of "image-container" to define each image in the list.

<ul id="portfolio-images">
    <li class="image-container"><img src="../someimage.jpg"></li>
    <li class="image-container"><img src="../someimage.jpg"></li>
    <li class="image-container"><img src="../someimage.jpg"></li>
    <li class="image-container"><img src="../someimage.jpg"></li>
    <li class="image-container"><img src="../someimage.jpg"></li>
    <li class="image-container"><img src="../someimage.jpg"></li>
</ul>