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

HTML How to Make a Website Debugging HTML and CSS Problems How to Fix Problems with Code

1,290 Points

Whats is the difference between a class and an ID?

I think I missed that detail in the lesson awhile back.



2 Answers

Not a whole lot. The biggest difference between the two is that you can only have one ID of a certain name on a particular page, whereas you can have as many classes with the same name as you want. In the CSS, classes are prefixed with a period (.), IDs with a hash (#).

For instance, I can use the class .nav as often as I want on a HTML page. But I can only use the ID #nav once.

Tommy Gebru
Tommy Gebru
30,164 Points

CSS selectors allow you to select and manipulate HTML element(s). The id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the id selector when you want to find a single, unique element. The class selector finds elements with the specific class. The class selector uses the HTML class attribute.