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
MOD
Michael Hulet
Treehouse Moderator 47,750 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
Treehouse Moderator 47,750 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