Home | All Questions | alt.html FAQ > CSS Basics

What is the difference between CLASS and ID attributes?

The CLASS attribute names a set (class) of elements that a particular element instance belongs to. The practical use of CLASS is currently in conjunction with style sheets, where you can then specify (using e.g. an elementname.classname selector) that some rules are to be applied to elements of a particular class. The "class" concept here means just any grouping you like. (The HTML 4.01 specification mentions that CLASS attributes might be used "for general purpose processing by user agents", but it's hard to see what this could mean, given that there is no specification on class names, and since the class names are in the author's name space, so to say.)

The ID attribute assigns a unique identifier (name) to a particular element instance. It too can be used in CSS, using e.g. elementname#id syntax for a selector, as you mention. But is has several other uses as well, even in practice, e.g. in scripting and as link anchors.

It is somewhat confusing, and things don't always work quite that way. but the basic point is that an ID is a unique identifier whereas CLASS is for creating sets.

Recommended Resources


Related Questions