Skip to content

Lesson 26 of 52Modern CSS

CSS Architecture

Why stylesheets turn messy as projects grow, and the habits that keep them sane, from resets and BEM to CSS Modules and utility classes.

17 min read0/4 questions

Writing CSS for one page is easy. Writing CSS that five people can change for two years without breaking things is hard. Every rule you write applies to the whole page by default, so a small change in one place can quietly break a screen nobody looked at. CSS architecture is the set of habits and tools that keep a growing stylesheet predictable. This lesson explains why CSS gets messy, then walks through the main approaches teams use: resets, naming conventions like BEM, scoped CSS Modules, and utility-first CSS, which leads straight into Tailwind. The next lesson, CSS Architecture for Design Systems, takes the same ideas to the scale of a whole company.

Why CSS gets messy

Three properties of CSS cause most of the pain.

  1. Everything is global. A rule like .title { color: red; } applies to every element with that class, on every page that loads the file. Two developers who both pick the name .title will fight without knowing it.
  2. Specificity wars. When a rule does not win, the quick fix is a more specific selector: .sidebar .card .title, then #main .sidebar .card .title, then !important. Each fix makes the next override harder. The stylesheet ratchets upward.
  3. Fear of deleting. You cannot easily tell whether a class is still used anywhere. So nobody deletes old CSS, and the file only grows. Teams call this an append-only stylesheet.

Every approach in this lesson attacks one or more of these problems: it either makes names unique, keeps specificity flat, or ties styles to the component that uses them so they can be deleted together.

Start with a reset and base styles

Browsers ship default styles: margins on headings and paragraphs, bullets on lists, a margin around body, form controls that ignore your font. Those defaults differ slightly between browsers. A reset removes the most annoying ones so you start from the same blank page everywhere. A small modern reset looks like this:

*, *::before, *::after {
  box-sizing: border-box;
}
 
* {
  margin: 0;
}
 
img, picture, video, svg {
  display: block;
  max-width: 100%;
}
 
input, button, textarea, select {
  font: inherit;
}

Each line fixes something real. border-box makes width include padding and border, so boxes are the size you asked for. Zeroing margins stops surprise gaps. Images never overflow their container. Form controls use your page font instead of a tiny system one.

On top of the reset come base styles: how plain elements look with no class at all. Body font and colour, heading sizes, link colour, a comfortable line height. Base styles use element selectors (h1, a, p), which keep specificity low. After that, every component only describes what makes it different.

Naming conventions: BEM

If every class name is global, the simplest defence is a naming system that makes collisions unlikely and tells you what a class belongs to. The most famous one is BEM, which stands for Block, Element, Modifier.

  • 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 hyphens: card--featured, card__title--large.
HTML & CSS
<style> .card { max-width: 260px; padding: 16px; border: 2px solid #ddd; border-radius: 12px; } .card--featured { border-color: #e4572e; background: #fff4ef; } .card__title { margin: 0 0 4px; font-size: 20px; } .card__meta { margin: 0 0 12px; color: #666; font-size: 14px; } .card__button { padding: 8px 14px; border: 0; border-radius: 8px; background: #222; color: white; }</style><article class="card card--featured"> <h2 class="card__title">Weeknight ramen</h2> <p class="card__meta">25 minutes, serves 2</p> <button class="card__button">Save recipe</button></article>

Preview

Remove card--featured from the article and the card goes back to normal. Notice that every selector is a single class. No nesting, no ids, no element selectors. Every rule has the same low specificity, so whichever is written later wins, and overrides stay easy.

The names are long, but they are honest. When you see card__meta in the HTML you know exactly which CSS file it lives in, and when you delete the card component you know which rules to delete too.

Quick check
In BEM, what is the correct class for a small variant of the price label inside a pricing table block?
Need a nudge? A hint rules out a wrong answer.

Keeping specificity low

Whatever naming you use, a few rules keep the cascade calm.

  • Style with classes, not ids. One id outweighs any number of classes, so an id selector is very hard to override later.
  • Avoid deep nesting. .page .sidebar .card h2 is tied to one exact HTML structure and is hard to beat. .card__title is not.
  • Do not style by element inside components. .card h2 breaks the day someone changes the heading to an h3.
  • Treat !important as an emergency tool. It wins over everything except another !important, which starts a new war.
  • Use :where() for defaults so they have zero specificity.
  • Use @layer to set the order of whole groups up front.

That last one gives you a clean, standard structure for any project:

Put it in order4 steps

Put these cascade layers in order, from weakest to strongest.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.

Scoped CSS: CSS Modules

Naming conventions rely on people being careful. Component-scoped CSS makes collisions impossible by letting a tool rename your classes. The most common version is CSS Modules, built into tools like Vite and Next.js.

You write a normal CSS file whose name ends in .module.css, next to the component that uses it:

Styles living next to components
  • src/
  • components/
  • Card.jsxthe component
  • Card.module.cssstyles only Card uses
  • Button.jsx
  • Button.module.css
  • styles/
  • reset.cssglobal, loaded once
  • base.cssglobal element styles
/* Card.module.css */
.card { padding: 16px; border-radius: 12px; }
.title { font-size: 20px; }
// Card.jsx
import styles from "./Card.module.css";
 
export function Card({ title }) {
  return (
    <article className={styles.card}>
      <h2 className={styles.title}>{title}</h2>
    </article>
  );
}

At build time, the tool rewrites .title to something unique like .Card_title__x7f2a, and styles.title holds that new name. Another component can also have a .title class and they will never clash. You get short names, no global leaks, and easy deletion: remove Card.jsx and Card.module.css together and nothing else breaks.

Other tools solve the same problem in different ways. Vue and Svelte have scoped styles inside each component file, and the browser's Shadow DOM keeps the styles of web components sealed off from the rest of the page.

Utility-first CSS

There is a completely different approach. Instead of naming components and writing new CSS for each one, you write lots of tiny classes that each do one thing, and combine them in the HTML. These are called utility classes.

HTML & CSS
<style> .p-4 { padding: 16px; } .rounded { border-radius: 12px; } .border { border: 2px solid #ddd; } .bold { font-weight: 700; } .muted { color: #666; } .mb-2 { margin: 0 0 8px; } .flex { display: flex; } .gap-2 { gap: 8px; }</style><div class="p-4 rounded border"> <p class="bold mb-2">Weeknight ramen</p> <p class="muted mb-2">25 minutes, serves 2</p> <div class="flex gap-2"> <span class="p-4 border rounded">Quick</span> <span class="p-4 border rounded">Vegetarian</span> </div></div>

Preview

Try building a second card below the first using only the classes that already exist. You do not write a single new line of CSS.

That is the appeal. The stylesheet stops growing, because new screens reuse the same small set of classes. Every utility has the same single-class specificity. You never invent a name. And deleting a piece of HTML deletes its styling with it, because the styling is in the HTML.

The cost is long class lists in your markup, and you need a full, consistent set of utilities for spacing, colour, type and layout. Writing and maintaining all of those by hand would be a big job. That is exactly what Tailwind CSS does for you: it generates a complete, well-designed utility set and only ships the classes you actually use. It is the subject of the next three lessons.

Match the pairs0/5 matched

Match each approach to the main problem it solves.

Choose an item, then its match on the other side.

Choosing an approach

ApproachGood forWatch out for
Plain CSS with BEMSmall sites, no build stepRelies on discipline
CSS ModulesComponent apps (React, Next.js)Sharing values between modules needs variables
Utility-first (Tailwind)Fast building, consistent designLong class lists, a learning curve
Scoped styles (Vue, Svelte)Projects in those frameworksFramework specific

Whatever you choose, the same goals apply: flat specificity, styles that live next to the thing they style, design values (colours, spacing, fonts) stored once as custom properties, and code you can delete without fear.

Quick check
A teammate fixes a style that would not apply by changing .title to #page .sidebar .title. What is the main long-term problem?
Need a nudge? A hint rules out a wrong answer.
Key terms
CSS reset
A small stylesheet that removes inconsistent browser defaults so every browser starts from the same place.
base styles
Styles for plain HTML elements (body, headings, links) before any classes are applied.
BEM
Block, Element, Modifier: a naming convention like card__title--large that keeps class names unique and readable.
CSS Modules
CSS files whose class names are rewritten to unique names at build time, so styles are scoped to one component.
utility class
A tiny class that does one job, like .p-4 for padding, combined with others in the HTML.
specificity war
Escalating selectors (more classes, ids, !important) to beat earlier rules, making each override harder.