Skip to content
Tutorials Center
My learning

Topics

Lesson 115 of 139 ยท Best Practices

Organizing CSS

Keep growing stylesheets manageable with BEM naming, utility classes, component styles, design tokens, cascade layers and a clear file structure.

A stylesheet with fifty rules is easy to manage. A stylesheet with five thousand rules, written by six people over three years, is a different story. Rules fight each other, nobody dares to delete anything, and every fix adds an !important. The CSS isn't wrong, exactly. It just has no structure.

This lesson is about that structure. You will learn the main ideas teams use to keep CSS predictable: naming conventions such as BEM, small single-purpose utility classes, component-based styles, design tokens, and an architecture built on cascade layers. There is no single right answer, but there are a few principles every good system shares, and by the end you will be able to choose a style that fits your project.

The problems we are solving

Most CSS pain comes from three sources:

  1. Name collisions. Two people write .title for two different things, and one quietly restyles the other.
  2. Specificity creep. A rule loses, so someone writes a longer selector to beat it. Then someone beats that one. Soon you have #main .content .sidebar ul li a.
  3. Fear of deleting. Nobody knows which HTML uses .box-2, so it stays forever.

Every approach below attacks these three. Keep them in mind and the reasons behind each convention become obvious.

BEM: Block, Element, Modifier

BEM is a naming convention. It doesn't need any tools; it is just a way of choosing class names:

  • A block is a standalone component: .card, .menu, .search-form.
  • An element is a part of a block, joined with two underscores: .card__title, .card__image.
  • A modifier is a variation, joined with two dashes: .card--featured, .button--small.
CSS
.card { }
.card__image { }
.card__title { }
.card__body { }
.card--featured { }
.card--featured .card__title { }

Because every class is unique and flat, specificity stays at a single class, (0,1,0), almost everywhere. Collisions vanish, since .card__title can't clash with .modal__title. And searching your HTML for card__ tells you exactly where a block is used, so deleting old code is safe.

A BEM card

The featured card is the same block with one extra modifier class. Notice there are no tag selectors and no IDs. If you later add an h2 elsewhere on the page, the card's title styles don't touch it.

With native CSS nesting you can group a block's rules together, but &__title does not build .card__title the way Sass does. Write the full class name inside the block: .card { .card__title { } }, or keep the rules flat.

Utility classes

The opposite approach is utility-first CSS. Instead of naming components, you create tiny classes that each do one thing:

CSS
.flex { display: flex; }
.gap-2 { gap: 0.5rem; }
.p-4 { padding: 1rem; }
.text-center { text-align: center; }
.rounded { border-radius: 8px; }

Then you build designs directly in the HTML by combining them: <div class="flex gap-2 p-4 rounded">. Frameworks such as Tailwind CSS take this to its logical end and generate thousands of utilities from a configuration file.

The trade-offs are real on both sides:

  • Pros: you almost never write new CSS, the stylesheet stops growing, and deleting HTML deletes its styles too, because nothing else uses that exact combination.
  • Cons: the HTML gets long and repetitive, and a design change to "all cards" means editing many templates unless you use a component system on the server or in JavaScript.

Many teams mix both: components for the big repeated patterns and a small set of utilities for one-off spacing and alignment.

Composing with utilities

Every bit of styling lives in the class attribute. Try changing bg-soft to bg-brand text-white on the outer div: no CSS edit needed.

Design tokens with custom properties

Whichever naming style you pick, don't scatter raw values everywhere. Collect colors, spacing, radii and font sizes as design tokens in custom properties:

CSS
:root {
  --color-text: #1d1f24;
  --color-brand: #1f6f54;
  --space-2: 0.5rem;
  --space-4: 1rem;
  --radius-md: 10px;
  --font-size-sm: 0.875rem;
}

.card {
  padding: var(--space-4);
  border-radius: var(--radius-md);
}

Tokens give you consistency (only a handful of spacing values exist), one place to change the brand color, and a natural hook for dark mode.

An architecture with cascade layers

Cascade layers let you decide, once, which group of styles beats which, regardless of specificity. A popular order, from weakest to strongest:

CSS
@layer reset, base, layout, components, utilities;
  • reset: removes browser inconsistencies (see Resets).
  • base: element defaults such as body, h1, a, using tokens.
  • layout: page-level structure, such as the header, sidebar and main grid.
  • components: cards, buttons, menus.
  • utilities: small overrides that must always win.

Because later layers win over earlier ones, a utility class like .mt-0 beats a component's margin even if the component selector is more specific. You no longer need !important to make utilities work.

Layers decide who wins

The component selector has much higher specificity than .text-danger, yet the utility wins, because the utilities layer comes later. Move utilities before components in the first line and run it again to see the result flip.

File structure

In a real project, split the CSS into files that mirror the layers, then combine them with a build tool or @import ... layer():

TEXT
styles/
  main.css          โ† declares layer order and imports the rest
  tokens.css
  reset.css
  base.css
  layout/
    header.css
    page-grid.css
  components/
    button.css
    card.css
    modal.css
  utilities.css
CSS
/* main.css */
@layer reset, base, layout, components, utilities;

@import url("reset.css") layer(reset);
@import url("base.css") layer(base);
@import url("components/card.css") layer(components);

One component per file makes ownership clear: to change the card, open card.css. In production, bundle the files together so the browser doesn't wait on a chain of @import requests; the CSS Performance lesson explains why.

Principles that matter more than the method

Whatever you choose, these habits keep CSS healthy:

  • Keep specificity low and flat. Prefer single classes. Avoid IDs in selectors and avoid deep nesting.
  • Style by class, not by structure. .card__title survives HTML changes; .card > div > h2 does not.
  • Separate layout from components. A card shouldn't set its own outer margin; the page that places it should.
  • Document conventions. A short README describing your naming and layers saves hours of debate. Use comments to label sections.
  • Be consistent. A mediocre convention followed everywhere beats a brilliant one followed half the time.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In BEM, what does .menu__item--active describe?

    menu is the block, __item is an element of it and --active is a modifier of that element.
  2. 2. Why does BEM help keep specificity low?

    Flat, unique class names mean you rarely need descendant chains, so specificity stays at (0,1,0).
  3. 3. With @layer reset, base, components, utilities;, why does .text-danger beat main .notice p.notice__text?

    Layer order is checked before specificity, so the utilities layer wins over the components layer.
  4. 4. What is a main drawback of utility-first CSS?

    Utilities keep the CSS small, but the styling moves into the class attributes of every template.

Summary

  • CSS architecture fights name collisions, specificity creep and fear of deleting code.
  • BEM names blocks, __elements and --modifiers to keep classes unique and specificity flat.
  • Utility classes do one job each and are composed in HTML; many teams mix them with components.
  • Design tokens in custom properties keep values consistent and themeable.
  • Cascade layers (reset, base, layout, components, utilities) settle conflicts without !important.
  • Organize files to mirror your layers, one component per file, and bundle them for production.

Next: Preprocessors and Frameworks.