← BYTES
HTML·Intermediate·Ref: 2.2.5

What is the difference between an element's `id` and `class` attributes?

Have a question about this video? Mention reference 2.2.5 when you contact us.

a.They are interchangeable with no functional difference
b.`id` should be unique to one element per page (used for direct targeting like anchor links or getElementById); `class` can be applied to many elements for shared styling/behavior
c.`class` can only be used for CSS, never JavaScript
d.`id` is deprecated in HTML5
An id is meant to uniquely identify a single element on the page (used for in-page anchor links #id, document.getElementById(), and label for associations); class is meant to be shared across many elements needing the same styling or behavior -- an element can have multiple classes but should have at most one id.

Find this on YouTube & Instagram

Every BYTES video is tagged with its reference number and topic — search any of these on YouTube or Instagram to find this one:

#BYTES#BYTES225#HTML