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.
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.
- 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.titlewill fight without knowing it. - 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. - 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.
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.
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 h2is tied to one exact HTML structure and is hard to beat..card__titleis not. - Do not style by element inside components.
.card h2breaks the day someone changes the heading to anh3. - Treat
!importantas 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
@layerto set the order of whole groups up front.
That last one gives you a clean, standard structure for any project:
Put these cascade layers in order, from weakest to strongest.
- Tap the steps below in the right order.
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:
- 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.
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 each approach to the main problem it solves.
Choosing an approach
| Approach | Good for | Watch out for |
|---|---|---|
| Plain CSS with BEM | Small sites, no build step | Relies on discipline |
| CSS Modules | Component apps (React, Next.js) | Sharing values between modules needs variables |
| Utility-first (Tailwind) | Fast building, consistent design | Long class lists, a learning curve |
| Scoped styles (Vue, Svelte) | Projects in those frameworks | Framework 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.
.title to #page .sidebar .title. What is the main long-term problem?- 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--largethat 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-4for padding, combined with others in the HTML. - specificity war
- Escalating selectors (more classes, ids,
!important) to beat earlier rules, making each override harder.