Is it always advisable to add both the ID and Class attributes to an element?

When writing HTML, is it advisable to always add both the ID and Class attributes to your HTML tags?

For CSS, a lot of times classes are preferred. ID’s can be very useful but you need to have a reason to use them.

@noblekc I’ll add, contextually, if you have similar items that have unique characteristics/behaviors that diverge at some point, then it’s useful to have both.

3 Likes

It depends on the situation. If you aren’t planning to use them, than I wouldn’t add them, unless it increases readability.
For most purposes it is best to use the least specific selector, which means class before id.

The only reason you should use ID is have you have a specific element you want selected, such as for styling or functionality.

2 Likes

Is it possible for one not to include the ID attribute during the early stage of writing the HTML and then later-on, they figure out that they needed the ID attribute for something?

in this case would it have been nice to have the both of them from the beginning?

A mixture of both is good, sometimes I will end build and style one section at a time so I know about what I need. But then other times it works out better to build the wholeHTML structure and then comeback and style it, so you may find out you need to add an ID here or there.

Plus when designing (it does not work this way for everyone) a good portion of the time your final product won’t be the same as you first planned. You may find a better way to do something, or decide you like a different layout for your page. In this case you may end up having to add extra classes and IDs.

1 Like

Okay, that makes sense. It doesn’t hurt to add both depending on one’s preferences, how they work and what they’re doing.

Thank you all.

1 Like