How to Name a CSS Class
• John Vandivier
This article recommends an approach to naming CSS classes.
tldr:
- Do this: .font-color-red {}
- Not this: .header {}
- Why? It's semantic and performant.
- A building has 5 attributes: A color, height, width, length, and smell. Let there be 3 possible values for each attribute.
- Every possible set of building styles could be described with (3 * 5) = 15 adjective-like rules, such as .red, .blue, and so on.
- To describe every permutation of building using noun-like rules would require 3^5 classes. Clearly, this will require more load and processing time. It's also not intuitive or developer-friendly.
- These class names are semantic. They are named according to their English meaning and therefore intuitively understood. This makes development easier.
- Adjective-like names are modular, performant, and DRY.
- They separate concerns. If something is named as an adjective it is not an element. If something is named as a noun it is not a style.*