CSS Best Practices

CSS Naming Conventions

Consistent, descriptive class names that make CSS maintainable and team-friendly.

How should I name CSS classes?

Name classes after what something is or does, not how it looks: .alert-error rather than .red-text, so the name stays true when the design changes. Use lowercase kebab-case, pick one convention such as BEM, and keep each selector to a single class where possible.

Naming Convention Examples

Code Editor

<!-- ✗ Named by appearance -->
<p class="red-text big">Error!</p>

<!-- ✓ Named by purpose -->
<p class="alert alert--error">Error!</p>
<button class="tab is-active">Active tab</button>

Preview

CSS Naming Conventions — step by step

1 / 4
1

Why Naming Matters

Good class names communicate intent — .button-primary is more maintainable than .blue-btn. Names should describe what something IS, not what it looks like.

1 of 4
💡Name What It Is, Not What It Looks Like
.success-banner is a better name than .green-banner — if you later change it to blue, the class name still makes sense. Semantic names survive design changes; presentational names do not.

🎯 Quick Check

Which class name follows semantic naming best practices?

CSS Naming Conventions: summary

Key points

  • Avoid generic names like .box or .left that collide across components.
  • Prefix JavaScript hooks (.js-toggle) or use data-* attributes so styling and behaviour don't depend on each other.
  • State classes such as .is-open and .has-error read clearly in both HTML and CSS.

Frequently asked questions

›Should CSS classes use camelCase or kebab-case?

Kebab-case (main-nav) is the CSS convention. camelCase is common only with CSS Modules, where classes are accessed from JavaScript.