Sub topic 5
How class names become locally scoped
This chapter assumes you can read a basic React component, know CSS selectors and specificity, and have a bundler (Vite, webpack, or Next.js) already set up. It explains what happens to a class name between your .module.css file and the browser.
The core idea: rename at build time
CSS has no built-in local scope. CSS Modules gets the same effect by renaming. During the build, a CSS processor reads each .module.css file and replaces every class name with a unique generated name. It rewrites the CSS and the JavaScript import to use the new names.
Start with this file:
.button {
padding: 0.5rem 1rem;
border: 0;
border-radius: 4px;
background: #2563eb;
color: #fff;
cursor: pointer;
}
.button:hover {
background: #1d4ed8;
}The component imports it:
import styles from './Button.module.css';
export function Button({ children }) {
return <button className={styles.button}>{children}</button>;
}The default import styles is a plain object. Its keys are your local class names. Its values are the generated names.
| Source | After the build |
|---|---|
| `.button` in `Button.module.css` | `.Button_button__x7Fq2` (example) |
| `styles.button` in the component | the string `"Button_button__x7Fq2"` |
The exact format depends on your tool and settings. Vite produces names like _button_1a2b3_1. Next.js uses a hash-based pattern. The pattern always combines the file, the local name, and a hash of the file path and name. Two files that both define .button therefore end up with different names, so their rules cannot collide.
What the build does
The browser never sees .button. It receives the rewritten stylesheet and elements whose class attribute holds the generated name. The match between them was made at build time, so no runtime library is needed.
What gets scoped, and what does not
Scoping applies to selectors that start from a class (or an id). Every class in a selector is renamed, including those in compound or nested selectors:
.card .title:hover {
color: #1d4ed8;
}Both .card and .title are renamed consistently. Other parts of the selector are left alone. Pseudo-classes, attribute selectors, and element names are not scoped.
- A bare element selector such as
h2 { margin: 0 }stays global. Most tools reject it in "pure" mode, and it affects everyh2on the page if allowed. - Keyframe names and custom properties are handled differently depending on configuration, so check your tool's docs before relying on them.
Consequences you can rely on
- A class name only has meaning inside the file that defines it. Different components can each use
.titleor.containerfreely. - Specificity is unchanged. Renaming does not make
.buttonstronger or weaker, so ordering and the cascade still decide conflicts between rules. - A typo like
styles.butonreturnsundefined, not an error. React then renders no class at all.
Checking the result yourself
Add console.log(styles) to the component. You will see the object with each local name and its generated value. Inspect the element in browser dev tools to confirm the same string is on the element.
Exercise
Two components each define .title in their own .module.css file. Why do the rules not affect each other, and what would styles.titel evaluate to?
Show answer
The build renames each .title to a different generated name, based on the file and a hash, so the rules target different classes. styles.titel is a misspelled key, so it evaluates to undefined and no class is applied.