Sub topic 1
Why global CSS gets hard to maintain
Prerequisites
This chapter assumes you know the following:
- Basic React: components, props and JSX.
- CSS fundamentals: selectors, the cascade and specificity.
- ES module imports.
You do not need to know CSS Modules yet. This chapter explains the problem they solve.
One namespace for the whole app
Plain CSS has a single global scope. Every rule you write applies to every matching element on the page. It does not matter which file the rule lives in or which component renders the element.
React encourages you to build small, isolated components. A component's JavaScript is isolated, because its variables stay inside its module. Its CSS is not. Once the stylesheet loads, its selectors compete with every other selector in the app.
A collision in practice
Suppose two developers each build a component. Both pick the obvious class name .title.
/* Card.css */
.title {
font-size: 1.25rem;
color: #111827;
}
/* Banner.css */
.title {
font-size: 2.5rem;
color: #b91c1c;
}import './Card.css';
import './Banner.css';
export function Card() {
return <h3 className="title">Card heading</h3>;
}
export function Banner() {
return <h1 className="title">Sale ends today</h1>;
}Both rules have the same specificity, so the cascade falls back to source order. The stylesheet that loads last wins. The card heading becomes large and red, even though nobody edited Card.css.
Why the problem grows
Collisions are only one symptom. As an app grows, global CSS causes several related problems.
- Order dependence. The result depends on the order stylesheets load. Bundlers can change that order when you split code, add a lazy-loaded route, or reorder imports.
- Specificity escalation. To beat a rule you cannot find or change, you write a more specific selector, such as
.page .card .title. Later you need an even more specific one, or!important. - Unsafe deletion. A class may be used by any component. You cannot tell whether a rule is dead, so nobody removes it. Stylesheets only grow.
- Unsafe edits. Changing a shared rule can break a screen you never opened.
- Leaking element selectors. A rule like
p { margin: 0 }affects every paragraph in the app, including those inside third-party components.
Common workarounds and their costs
Teams have tried to manage this with discipline instead of tooling.
| Approach | How it helps | Cost |
|---|---|---|
| Naming conventions such as BEM (`.card__title`) | Makes names more unique | Long names, and uniqueness depends on everyone following the rule |
| Deep selectors (`.card .title`) | Narrows where a rule applies | Raises specificity and couples CSS to markup structure |
| `!important` | Forces a rule to win | Makes later overrides harder |
| One shared stylesheet with review | Centralizes control | Becomes a bottleneck and a large file |
All of these rely on people. A convention that works with three developers often breaks with thirty.
What we want instead
The workarounds point to a clear goal. A component's class names should be local to that component by default. Sharing should be something you choose, not something that happens by accident. A tool, not a naming convention, should guarantee that two components cannot collide.
The next chapter introduces CSS Modules, which provide this guarantee at build time.
Exercise
In the Card and Banner example, Card.css is never edited, yet the card heading changes appearance. Name the cause and give two reasons why a bundler change could make the result differ.
Show answer
Both files define .title with equal specificity in one global scope, so the cascade resolves the conflict by source order, and the later stylesheet wins. A bundler could change the load order through code splitting (for example a lazy-loaded route that loads one stylesheet later) or through reordered imports, which would flip which rule wins.
Exercise
Why does raising specificity (for example .page .card .title) fail as a long-term fix for collisions?
Show answer
It does not remove the shared namespace. It only wins the current conflict, and later conflicts then need even more specific selectors or !important. It also ties the CSS to the markup structure.