Best Practices: Classes & IDs

When to use which?

Ask yourself: "is there more than one element which requires the same style, now or at any time in the future?

Yes? -> Use a class

Maybe? -> Use a class

No? -> Use an ID

How to name them?

Thinking of an understandable name for each class or ID is hard.

Choosing a good name can save you (and your team) time in the future, reading and maintaining code.

There are many methodologies that different teams use to standardise how they name classes and IDs in CSS. For example: BEM, which is outlined below

But for now, here are a few very simple pointers:

Block-Element-Modifier (BEM) Method

Some methods, such as BEM use specific text delimiters.

BEM stands for Block, Element, Modifier.

In the class name, the first word represents the block the element belongs to, the second word represents the element itself, and the third (if needed) is a word to describe what has or could change, the modifier.

See the image below for an example: card__button--success{

BEM method CSS

Image source

Click here to learn more about the BEM naming methodology: