I wanted to know the basic difference between id and class attributes and when to use according to the situation. Got doubt while solving this HTML exercise
Hey @object8438290220, welcome to the forums!
class and the
class or an
id to that button to later select it in CSS
These were the similarities, however, there are some slight differences.
idattribute must be unique meaning only one element can have a specific
idwhile many elements can have the same
This is the only difference you’ll understand right now, however, when you get onto CSS, you’ll see that
id selectors have a higher specificity than
Until then, try to use
class attributes even when you want to select one specific element. It’s always better to use the lowest specificity you can
Hey @object8438290220 to add on to the other reply, here is a fun site that visualizes the differences in specificity for you to check out