What determines whether you use an id or a class to style elements?

I’m just wondering what is the thought process when deciding to use an id or a class to style an element, given they both can be used to style?

Hi @El_Escandalo.
You use an id if the style has to be applied to only one element (the id value can’t be repeated in the page).
You use a class if the style is shared among multiple elements (the ones that share said class).

1 Like

It is mostly preference but there is one fundamental difference. Ids are unique to elements (multiple elements can’t have the same id) but different elements can have the same class attribute. So it’s better to use class attribute (by assigning each of them the same class) if you want to style different elements the same way. If you are interested in learning more about ids and classes, when and why you should choose one over the other, follow This Stackoverflow Thread
Hope that helps!

1 Like

It’s a very long thread. To get the gist of it, you will have to read through up to the bottom.

1 Like