CSS CSS Basics (2014) Basic Selectors Reusing Classes

ID vs Classes

Since we are able to use class for more than one element as oppose to ID. Is it okay to use class and not ID? Just to keep things simple.

3 Answers

Kevin Korte
Kevin Korte
28,100 Points

I'll also add that classes and IDs hold different CSS "point values", which on larger projects can lead to some really gnarly specificity problems.

I highly recommend you read this: https://css-tricks.com/specifics-on-css-specificity/

and use IDs responsibly. My personal workflow is that I also default to using a class to style. If I need an ID to style, I need to make a valid point why. For the most part I attach jQuery calls to element IDs.

jamescool
jamescool
44,399 Points

You're welcome! One more thing might be important to point out:

It's not merely the case that there must be only one instance of a specific ID per document, it's ALSO true that each element can have only one ID. Conversely, an element can have multiple classes:

<p class="large-12 columns"> placeholder text </p>

In the above example, the p element has two classes: "large-12" and "columns" (each separated by a space). This cannot be done with IDs.

jamescool
jamescool
44,399 Points

Yes. The only unique role of an ID is that since there must be only one per document, it's a better way to ensure that only one item is being targeted.

Thank you James! I appreciate the quick response!