Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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>