
Good CSS class names help you find the right rule when a design changes. They also tell the next person who opens the stylesheet what an element does. This guide compares common naming conventions with examples you can use in your own project.
1. Best practices for naming CSS classes
1.1. Choose a consistent word separator
Both .header-menu and .header_menu are valid. Pick a convention for ordinary class names and apply it throughout the project. Many teams use lowercase words separated by hyphens.
1.2. Keep capitalization predictable
If a team uses .headerMenu, follow its style. Keep capitalization identical in HTML and CSS. Avoid collapsing several words into a name such as .headermenu when a separator makes it easier to scan.
1.3. Favor short, descriptive names
.header-menu tells the reader more than .hm. Name a component for its role. A .primary-button can stay the primary button even when the design stops using red or changes its size.
1.4. Reuse classes where the role is shared
A .link class makes sense if several parts of a site share one link style. A name such as .footer-link is useful when footer links have genuinely different treatment. Let the component’s role determine how specific the name needs to be.
2. CSS naming conventions and examples
2.1. BEM (Block, Element, Modifier)
A block is a standalone component. An element belongs to that block. A modifier expresses a variation of a block or element. The following example uses the common double-underscore and double-hyphen convention. Apply a modifier alongside the base class.
.header { /* Block */ }
.header__logo { /* Element */ }
.header__logo--large { /* Modifier */ }
2.2. OOCSS (Object-Oriented CSS)
Separate reusable structure from a component’s appearance. For example, give every button a base class, then add a separate class to set its visual emphasis or size.
.button { /* Shared button structure */ }
.button-primary { /* Primary appearance */ }
.button-large { /* Larger size */ }
2.3. SMACSS (Scalable and Modular Architecture for CSS)
SMACSS groups rules by purpose, including base styles, layout, modules, states, and themes. A state class can make it clear that an item changes as the interface is used.
.menu { /* Module */ }
.menu-item { /* Part of the module */ }
.menu-item.is-active { /* Active state */ }
2.4. Atomic CSS
An atomic or utility class applies a small, reusable style. Combine utility classes when you need several independent properties on one element.
.text-center { text-align: center; }
.bg-blue { background-color: #2154a4; }
.m-2 { margin: 0.5rem; }
2.5. Semantic class names
Semantic class names describe the purpose of an element. Names such as .primary-button, .navigation-menu, and .profile-avatar remain meaningful even when the visual design evolves.
Follow the convention your team already uses. If you are starting from scratch, document how you name components, variants, and states, then try the convention on a few real examples before applying it across the project.