Skip to content

Courses / CSS3 Mastery: Beginner to Advanced / Naming Conventions

2,087 people viewed this

Naming Conventions

Section 32: CSS Naming Conventions — BEM and Utilities

BEM is a naming convention that makes component relationships and variations clear. Utility-first styling uses small classes for individual styling responsibilities.

1. Understand BEM

  • Block: a standalone component, such as card.
  • Element: a part of the block, such as card__title.
  • Modifier: a variation, such as card--featured.
.card { padding: 1rem; }
.card__title { font-size: 1.25rem; }
.card__description { color: #475569; }
.card--featured { border: 2px solid #2563eb; }

2. Utility classes

.text-center { text-align: center; }
.mt-2 { margin-top: .5rem; }
.rounded { border-radius: .5rem; }
.shadow-sm { box-shadow: 0 1px 3px #0002; }

Utility classes are intentionally small and can be combined in markup. A utility-first framework such as Tailwind provides a large predefined utility system, while a project can also create its own utilities.

3. Avoid vague names

/* Vague */
.blue { color: blue; }
.big { font-size: 2rem; }

/* Purpose-oriented */
.notice__link { color: #1d4ed8; }
.page-title { font-size: 2rem; }

Choose names based on component or purpose rather than appearance alone when that makes future changes easier. A class like .blue becomes confusing if the design color changes.

Runnable example

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BEM Example</title>
<style>
.product-card { max-width: 320px; padding: 20px; border: 1px solid #cbd5e1; border-radius: 12px; font-family: Arial, sans-serif; }
.product-card__title { margin-top: 0; }
.product-card__price { color: #166534; font-weight: bold; }
.product-card--featured { border: 2px solid #2563eb; }
.text-center { text-align: center; }
</style>
</head>
<body>
<article class="product-card product-card--featured">
  <h2 class="product-card__title">Laptop Stand</h2>
  <p>Adjustable aluminum stand.</p>
  <p class="product-card__price">₹999</p>
</article>
</body>
</html>

Common mistakes

  • Using different naming conventions randomly across the same codebase.
  • Writing selectors that are unnecessarily long or tightly coupled to HTML nesting.
  • Using names such as .red-text when the class actually represents an error message.
  • Assuming BEM or utility classes are mandatory browser features; they are conventions.

Practice exercise

Create a notification component using BEM with title, message, and close-button elements. Add success and error modifiers.

Knowledge check: 10 MCQs

  1. What does BEM stand for? Answer: Block, Element, Modifier. Explanation: These three concepts describe components, their parts, and their variations.
  2. Which is a typical BEM element? Answer: card__title. Explanation: The double underscore identifies an element of the card block.
  3. Which is a typical BEM modifier? Answer: card--featured. Explanation: The double hyphen identifies a variation.
  4. What is a utility class? Answer: A small class with a focused styling purpose. Explanation: Examples include text alignment or spacing utilities.
  5. Are BEM names understood specially by browsers? Answer: No. Explanation: They are naming conventions for developers.
  6. Why can .blue be a weak class name? Answer: It describes appearance rather than purpose. Explanation: The class may become misleading after a design change.
  7. Can one element have multiple classes? Answer: Yes. Explanation: HTML elements can combine component, modifier, and utility classes.
  8. Does utility-first styling require a specific framework? Answer: No. Explanation: Utilities can be written in ordinary CSS.
  9. What does a modifier usually represent? Answer: A component variation or state. Explanation: It can describe featured, disabled, or error styling.
  10. What is the main benefit of consistent naming? Answer: Easier reading and maintenance. Explanation: Developers can understand the role of a rule more quickly.

Rate this course

No ratings yet — yours can be the first.

Stars show on this course. Your written note stays private.

Your stars