Classes and ids

Knowledge Videos

Short animated explainers, organised by subject.

Classes and ids

A class is a name many elements can share, an id belongs to one element. The dot and hash selectors, and div and span as plain containers.

Transcript

So far, a rule styles every h one or every p. But what if you want to style only some elements? Give them a class.

A class is a name you add with the class attribute. Many elements can share the same class. In C S S, a class selector starts with a dot.

An id is a unique name. Only one element on the page can have a given id. In C S S, an id selector starts with a hash sign.

The div element is a plain container for blocks, and span is a plain container for a bit of text. They have no style of their own, so classes and ids are how you style them.

Remember: a class is shared by many elements, an id belongs to just one, and div and span are plain containers.

More in this series