Sub topic 1
Why CSS gets hard to maintain at scale
Prerequisites
This chapter assumes you know:
- CSS selectors, properties and the box model.
- The basics of the cascade, inheritance and specificity.
- HTML, and how to link a stylesheet to a page.
- Basic command-line and npm usage. This matters in later chapters.
The problem in one sentence
CSS is global by default. Every rule you write can affect every element on every page, so the cost of a change grows with the size of the codebase.
A small site hides this. With one author and a few hundred lines, you can keep the whole stylesheet in your head. With several teams, dozens of pages and years of history, nobody can. This chapter names the forces that cause the pain. Later chapters cover the fixes.
Three forces that compound
Global scope
A selector like .button matches every element with that class, wherever it appears. Two developers can pick the same class name for different things and never notice until one component breaks the other. Removing a rule is risky for the same reason. You can't easily prove that nothing else relies on it.
Specificity
When two rules target the same element, the more specific selector wins. Selectors like .sidebar .button beat .button, and an ID selector beats both. Once a codebase has a mix of specificities, the only way to override a rule is often to write a more specific one. Each override raises the bar for the next.
Source order
When specificity is equal, the rule that appears later wins. Order therefore depends on how files are concatenated and which stylesheet loads last. Reordering two imports can silently change how a page looks.
What the escalation looks like
The following three files show a typical history. Each change made sense when it was written.
/* base.css */
.button {
background: blue;
color: white;
}
/* page.css, loaded later */
.sidebar .button {
background: gray;
}
/* hotfix.css, loaded last */
.button {
background: red !important;
}Inside .sidebar, the button is gray, not blue, because .sidebar .button is more specific than .button. Then someone needed red buttons everywhere and found that a plain .button rule would not beat the sidebar rule. They reached for !important. Now the only way to override that red is another !important with equal or higher specificity, or a later rule. The stylesheet has become a record of arguments instead of a design.
The loop in the diagram explains why the problem accelerates. Fear of deleting rules leaves dead code in place. Dead code makes the cascade harder to reason about, which makes people add more overrides.
Symptoms to watch for
- Files only grow. Nobody removes rules.
- New rules need stronger selectors than the ones around them.
!importantappears in application code, not just in utilities or overrides for third-party widgets.- A change in one component alters an unrelated page.
- Developers add a new class instead of editing an existing one, because they can't tell what the edit would affect.
- Stylesheet load order matters, and nobody has written down why.
What the rest of the tutorial does about it
Each force has a matching control. The table shows where each is covered.
| Force | Control | Where it is covered |
|---|---|---|
| Global scope | Naming conventions, scoping techniques | Naming, BEM, CSS Modules, Shadow DOM, `@scope` |
| Specificity | Flat selectors, linting | BEM, Stylelint rules |
| Source order | File structure, `@layer` | File organisation, layered architectures, cascade layers |
None of these remove the cascade. They make it predictable, so that you can say why a rule wins without opening the browser's developer tools.
Exercise
In the example, why does the button inside .sidebar stay gray even though base.css loads first and hotfix.css loads last, before !important is added? Name the two forces involved.
Show answer
Specificity decides it first. .sidebar .button has two class selectors, while the later .button rule has only one, so the gray rule wins regardless of order. Source order only matters between rules of equal specificity. The !important flag then overrides both forces, which is why it starts an escalation.