CSS CSS Basics Basic Selectors Class Selectors

Why use ID instead of Class?

Looking at the parameters for both these selectors, I can't see the benefit of using ID when Class can be used to apply the same changes to every instance in the document. Is there a marked reason to use ID instead?

5 Answers

Steven Parker
Steven Parker
203,995 Points

ID is a better choice when there is only one item to be targeted, since every element ID on the document must be unique.

Fran ADP
Fran ADP
6,288 Points

It is a better choice.

Muhammad Khan
PLUS
Muhammad Khan
Courses Plus Student 2,409 Points

Hi there, ID is a better choice when unique element to be targeted.

Kirt Perez
Kirt Perez
4,778 Points

What's wrong with using only one element that has a class that is only used once? Why not just stick to just class selectors?

Steven Parker
Steven Parker
203,995 Points

Technically, a class will do the job just fine; but using an ID makes it clear at first glance at the CSS that the rule targets one unique element.

Muhammad Khan
PLUS
Muhammad Khan
Courses Plus Student 2,409 Points

Hi there, an id can be used to identify one element, whereas class can be used to identify more than one. id is unique. So, id is a better choice when unique element to be targeted.