Tips・When you get stuck

How to stop agonizing over class names

red, box2, aaa — class names are hard because you're naming what things look like. Name them by role instead, and learn BEM, the convention that removes the rest of the guesswork.

red, box2, aaa — class names are hard because you’re trying to name what something looks like. Name it for what it’s for instead, and most of the agonizing disappears.

Naming by appearance hurts later

.red {
  color: #e5484d;
}

Looks fine, until you decide it should be blue.

<!-- The class says red, the text is blue: welcome to hell -->
<p class="red" style="color: blue;">On sale</p>

Name it by role and the name survives the color change.

.price-sale {
  color: #e5484d;
}
✕ Appearance○ Role
.red.price-sale
.big-text.section-title
.left-box.profile-card
.mt20.section

Ask yourself “what is this?” and write down the answer. That’s the whole method.

BEM — the convention that removes the guesswork

Name things freely for long enough and you end up with a pile of near-identical names. The convention used worldwide is BEM.

block__element--modifier
PartMeaningExample
BlockA standalone componentcard
ElementA part inside it, joined with __card__title
ModifierA variation or state, joined with --card--wide
The outer component is card; the parts inside are card__title and card__text. The name alone tells you what a part belongs to.

In practice

<div class="card">
  <h3 class="card__title">Shima</h3>
  <p class="card__text">Lives in Snowland.</p>
</div>

<div class="card card--highlight">
  <h3 class="card__title">News</h3>
  <p class="card__text">I drew something new.</p>
</div>
.card {
  border: 1px solid #ddd;
  border-radius: 12px;
  padding: 16px;
}
.card__title {
  font-size: 18px;
  margin: 0 0 8px;
}
.card--highlight {
  border-color: #ff8a3d;
  background: #fff8f3;
}

The key move: when you add a variation, keep the original class and add to it (class="card card--highlight"). Shared styling stays on card, and only the difference lives on card--highlight.

Loose is fine. Consistent is what matters

You don’t have to follow BEM to the letter. On a personal site, these three go a long way:

  1. Name by role (price-sale, not red)
  2. Share a prefix within a component (hero, hero-title, hero-image)
  3. Don’t mix conventions in one site (pick camelCase or kebab-case and stay)

The painful thing isn’t having no rule — it’s having several rules at once.

class or id?

classid
How many times?As many as you likeOnce per page
Mainly forStylingIdentifying from JavaScript, anchor links
CSS strengthNormalToo strong (hard to override later)

Styling goes on classes. Style through an id and you’ll fight to override it when you want a small variation.

CSSDecorate "on target" with classesClass selector basics, in the CSS courseHTMLIn-page linksUsing ids for in-page links

Characters you can use

  • Lowercase letters and hyphens as the base (profile-card)
  • Digits are fine in the middle or at the end (step2), but a name can’t start with a digit (2step is invalid)
  • Avoid spaces and non-Latin characters

Summary

  1. Name classes by role, not appearance (price-sale, not red)
  2. BEM is block__element--modifiercomponent, part, variation
  3. Consistency beats perfection. Style with classes; save ids for JavaScript and anchor links

CSS with good names still makes sense to you a month later. That’s the payoff.

FAQ

Can class names contain non-Latin characters?
Technically yes, but avoid it. Like file names and URLs, they invite encoding problems and typos. English words read better later than transliterations do.
Do I have to use two underscores and two hyphens in BEM?
On your own site one of each works, but there's a reason for two. It visually separates word breaks (main-visual) from part breaks.
When should I use id instead of class?
Use class for styling, and id for identifying a single element from JavaScript or as an anchor target. An id can only be used once per page.