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

CSS CSS Foundations Selectors Class and ID Selectors

Rishi Reddy
Rishi Reddy
3,894 Points

CSS vs. ID

What's the benefit of using ID's vs. Classes for styling other than the benefit of linking?

5 Answers

ID's should be unique and used once, whereas classes should be used across multiple elements. See below

#social { // Some styles }

.btn { // Some styles }

As you see the ID social would probably only be used once per page, and the class BTN can be applied to any button.

Also ID takes precedence over classes link

Chris Coyier has a good article about this.

Rishi Reddy
Rishi Reddy
3,894 Points

but can't you just create a class and use it once. what's the benefit of id then.

You can use classes over and over again, for instance a button let's say you want 3 buttons on your website with the same markup just use the same class.

An ID is can be considered unique on the page.

You can use classes over and over again, for instance a button let's say you want 3 buttons on your website with the same markup just use the same class.

An ID is can be considered unique on the page.