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 Basics (2014) Basic Selectors Reusing Classes

alan ingram
PLUS
alan ingram
Courses Plus Student 3,979 Points

Multiple classes and IDs

In the Reusing Classes videos I see you mentioned not to apply both IDs and Classes to the same tag. Forgive me if it blew past me but did or is there a video where you talk about assigning multiple classes or can you assign multiple IDs to an element in the markup. I know I have seen it before. Just not sure on its use-case or level of being a suggested method. If you could touch on that please. example being stuff like: <img src="folder/pic.jpg" class="sports-pic steve-pics">

I believe thats how I have seen it before.

2 Answers

Michael Hulet
Michael Hulet
47,912 Points

You can definitely assign multiple classes to a tag. Take the tag you referenced, for example:

<img src="folder/pic.jpg" class="sports-pic steve-pics">

This element has 2 classes: sports-pic and steve-pics. To give an element multiple classes, you just separate each class you want to give it with a space

However, an element cannot have multiple ids. There can only be 1 instance of any particular id in the whole document, so it only makes sense that each element can have at most 1 id

alan ingram
PLUS
alan ingram
Courses Plus Student 3,979 Points

So then this would be bad practice or invalid: <img src="folder/pic.jpg" id="sports-pic steve-pics">

Michael Hulet
Michael Hulet
47,912 Points

That would be invalid. How that would be parsed would depend on which browser you're rendering it in, but the way it's officially supposed to be treated is undefined