HTML & CSS · Junior · Question 8 of 75
What is the main difference between the id and class attributes?
- ✓Correct answer:
idmust be unique on the page and has higher CSS specificity;classcan repeat - 2
classmust be unique on the page, whileidcan be repeated as often as you like - 3
idis used only in JavaScript, whileclassis used only for CSS styling
Explanation
An id value must be unique in the document: anchors (#section), label for and ARIA attributes reference it. Classes are reused across many elements, and one element can have several. An id selector weighs (1,0,0) versus (0,1,0) for a class, which is why classes are usually preferred for styling.