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 How to Make a Website Responsive Web Design and Testing Build a Three Column Layout

John Silverstein
John Silverstein
6,290 Points

I thought u cant give an html element more than one id, and u need to use classes for that.

If its possible to give the same type of html element more then one id, then why use classes at all?

Did you see an example of an element getting more than 1 id?

2 Answers

Samuel Ferree
Samuel Ferree
31,722 Points

You can have multiple ID's that refer to the same HTML element (Although this is considered bad practice, and "Code Smell")

But you can't have a single ID that refers to multiple HTML Elements. this is the main use for classes.

<div class="round">
  This div will have the styles applied from .round
</div>
<div class="round">
  This div will also have those styles applied
</div>
Samuel Ferree
Samuel Ferree
31,722 Points

To clarify, an XHTML element can have multiple ids, e.g.

<p id="foo" xml:id="bar">

Again, this is bad practice, and "Code Smell"

Kevin Korte
Kevin Korte
28,148 Points

You are correct, you should only have one id per element. ID's and classes also have different css specificity, which is why it's so common to find classes as the main selector for css styles, as they have 1 point, vs an id that carries 10 points - the goal is to always keep css specificity as low as possible.