Skip to content
Tutorials Center
My learning

Topics

Lesson 123 of 139 ยท Best Practices

CSS Architecture: BEM, ITCSS and Utility Classes

Learn naming and layering strategies for large CSS codebases: BEM naming, ITCSS layers mapped to @layer, class prefixes and utility classes, with working examples.

A small stylesheet organizes itself. A stylesheet with 20,000 lines and ten contributors does not. Names collide, selectors get stronger and stronger to win arguments, nobody dares to delete anything, and every change breaks something three pages away. CSS architecture is the set of conventions a team agrees on to prevent that.

Organizing CSS introduced the basics: BEM, utilities, tokens and cascade layers. This lesson goes further into the two ideas that scale best, naming (BEM and its relatives) and layering (ITCSS), and shows how to combine them with modern CSS. You will build a small system and then look at the rules that keep it healthy as it grows.

Two separate problems

Large codebases suffer from two different problems, and different tools address each:

Problem Symptom Tool
Naming and scope .title in two places styles both A naming convention (BEM)
Ordering and specificity Adding a rule changes unrelated pages; !important everywhere A layering model (ITCSS)

Keep them apart in your head. A great naming scheme will not save you from specificity wars, and perfect layering will not stop two developers inventing .btn twice.

BEM, the naming half

Block, Element, Modifier gives every class a name that tells you where it belongs:

  • Block: a standalone component (card).
  • Element: a part that only makes sense inside a block (card__title).
  • Modifier: a variation of a block or element (card--featured, card__title--large).
HTML
<article class="card card--featured">
  <h2 class="card__title">Launch day</h2>
  <p class="card__text">Everything is ready.</p>
  <a class="card__link" href="#">Read more</a>
</article>

The strength of BEM is that every selector is a single class, with specificity (0,1,0). Rules can be moved around freely, because nothing depends on ancestors. A few habits keep it healthy:

  • Do not chain elements: write card__title, never card__body__title. The second suggests structure that will change. Elements belong to the block, not to each other.
  • A block never sets its own outer position. Margins around a card belong to whatever places it, such as a layout class or a Grid parent. Otherwise the block is awkward to reuse.
  • A modifier adds to the base, so the HTML carries both: class="card card--featured".
A BEM card and its modifier

Notice that the last rule, .card--featured .card__title, has specificity (0,2,0). That is fine, but each such rule is a small step toward specificity creep. When the modifier needs to restyle many elements, consider a card__title--featured class instead, or custom properties that the modifier changes, as in the --brand ring above.

OOCSS and SMACSS, in two lines each

You will meet these names in older material. They shaped how BEM and ITCSS came to be:

  • OOCSS (Object-Oriented CSS) says: separate structure from skin, and separate container from content. Make a .media layout object and a .card skin, and combine them, instead of one class doing everything.
  • SMACSS sorts rules into categories: base, layout, module, state, theme. Its idea of state classes (is-open, is-active, has-error) still works well for things JavaScript toggles.

ITCSS, the layering half

ITCSS (Inverted Triangle CSS) orders your stylesheet from the lowest-specificity, widest-reaching rules at the top to the highest-specificity, narrowest rules at the bottom. Each layer is more specific and affects fewer elements than the one before:

flowchart TD
  S["1 Settings<br/>variables, tokens"] --> T["2 Tools<br/>mixins, functions"]
  T --> G["3 Generic<br/>reset, box-sizing"]
  G --> E["4 Elements<br/>h1, a, button defaults"]
  E --> O["5 Objects<br/>layout patterns: .o-container"]
  O --> C["6 Components<br/>.c-card, .c-nav"]
  C --> U["7 Utilities<br/>.u-hidden, .u-mt-4"]

Reading down the list, rules get more specific and reach fewer elements. Later layers may override earlier ones safely.

The key rule: a rule may only depend on layers above it. A component can rely on tokens and element defaults, but an element default must never know about a component. That one-way flow is what makes the cascade predictable.

ITCSS with native @layer

When ITCSS was invented, the only way to enforce the order was file order and discipline. Today cascade layers enforce it for you, whatever the specificity of individual selectors. Tools and settings layers disappear (they were Sass things; tokens become custom properties), and you declare the rest up front:

CSS
@layer generic, elements, objects, components, utilities;

The next example uses layers in the order above. Look closely at the last two lines of the page: the utilities rule has the lowest specificity, yet it wins.

ITCSS order enforced with cascade layers

Without @layer, the strong component selector would beat the utility, and you would reach for !important. With it, the utility works the way every team expects utilities to work.

Prefixes make layers visible

Many ITCSS projects add a one-letter prefix so a class tells you its layer on sight:

Prefix Meaning Example
o- Object (layout pattern) o-container, o-stack
c- Component c-card, c-nav
u- Utility u-hidden, u-mt-4
is- / has- State, toggled by JavaScript is-open, has-error
js- JavaScript hook, never styled js-modal-trigger
t- Theme t-dark

The js- convention is a quiet but powerful one: designers can rename or delete style classes without breaking scripts, because scripts only look for js- names (or data attributes). Prefixes are optional, but in a team of ten they save daily arguments.

Utilities in a layered system

Utilities sit at the end of the cascade on purpose. They are tiny, single-purpose, and meant to override. A good utility layer:

  • Contains only classes that do one job, named for the effect: u-visually-hidden, u-truncate, u-text-center.
  • Uses tokens, not raw values, so spacing utilities (u-mt-2) share the same scale as components.
  • Stays short. If you find yourself adding the hundredth utility, consider whether a utility framework (see CSS Frameworks) would be a better foundation.

State and variants: pick one convention

Components change on interaction or data. Choose how you express that, and be consistent:

  • State classes (is-open) for JavaScript-driven state.
  • ARIA or data attributes ([aria-expanded="true"], [data-state="open"]) when the state already has meaning for assistive technology. Styling the attribute guarantees the style and the accessibility state cannot drift apart. See Accessibility.
  • Modifiers (c-card--featured) for variations that are decided when the HTML is written.

Rules that keep an architecture alive

  1. One component, one file (or one @layer components block per file). Delete the file, delete the component.
  2. No qualifying selectors: div.card and ul.nav hurt reuse. Use the class alone.
  3. Avoid IDs for styling; an ID adds a specificity jump nothing can undo (see Specificity).
  4. No !important outside utilities, and with layers, hardly even there.
  5. Do not style based on location (.sidebar .card) unless you accept that the card now has two homes. Use a modifier or a custom property instead.
  6. Document the layer order in one visible place, ideally the first line of the main stylesheet.
  7. Measure. Keep an eye on performance: file size, unused rules and specificity graphs reveal drift early.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which problem does a naming convention such as BEM mainly address?

    BEM gives each class a name that shows its block, so two parts of a page do not share a generic name like .title.
  2. 2. In ITCSS, what is the direction of dependency between layers?

    Components can build on elements and tokens, but element defaults must not know about components.
  3. 3. With @layer components, utilities;, a utility with specificity (0,1,0) and a component rule with (0,3,1) both set color. Which wins?

    Layer order is compared before specificity, so the utilities layer wins.
  4. 4. Why do many teams use js- prefixed classes?

    Scripts that only select js- names keep working when designers rename or remove style classes.
  5. 5. Which BEM name is better practice?

    Elements belong to the block, not to each other, so the structure can change without renaming classes.

Summary

  • Large codebases have a naming problem and a specificity problem; BEM and ITCSS address them separately.
  • BEM keeps selectors to a single class: block, __element, --modifier, no element chains.
  • ITCSS orders rules from low to high specificity: settings, tools, generic, elements, objects, components, utilities.
  • Native @layer enforces the ITCSS order, so a weak utility selector can still beat a strong component selector.
  • Prefixes (o-, c-, u-, is-, js-) make a class's role visible in the HTML.
  • Prefer ARIA or data attributes for state, avoid IDs and qualifying selectors, and document the layer order.

Next: Project: Profile Card.