Sub topic 2
What CSS Modules give you
This chapter assumes you know basic React (components, props, JSX), CSS selectors and specificity, and ES module imports. It also assumes you have used a bundler such as Vite, webpack or Next.js. The previous chapter covered why global CSS gets hard to maintain. This one covers what CSS Modules do about it.
The core idea
A CSS Module is an ordinary CSS file whose class names are local to the file that imports it. You name the file with a .module.css suffix. At build time, the bundler rewrites each class name into a unique one. It also gives your JavaScript an object that maps your original names to the new ones.
There is no runtime library and no new syntax to learn. You write plain CSS and import it like any other module.
.button {
padding: 0.5rem 1rem;
border: 0;
border-radius: 4px;
background: #2563eb;
color: #fff;
cursor: pointer;
}
.button:hover {
background: #1d4ed8;
}Save this as Button.module.css. Then import it in the component:
import styles from './Button.module.css';
export function Button({ children }) {
return <button className={styles.button}>{children}</button>;
}The default import, styles, is an object. Its keys are the class names you wrote. Its values are the generated names. styles.button might evaluate to something like Button_button__3xK9a. The exact format depends on your tooling.
What happens at build time
The bundler processes the CSS file and the JavaScript import together. It renames every local class in the CSS output, and it exports the mapping to your component.
In the browser, the rule and the element both use the generated name. Another file can also define .button. It receives a different generated name, so the two rules never collide.
What you gain
- Local scope by default. You can name a class
.titleor.buttonin every component without worrying about other files. - Predictable deletion. If a component is removed, its CSS file goes with it. Nothing else can depend on those class names by accident.
- Explicit dependencies. A component's styles come from an import you can see, not from a stylesheet loaded somewhere else.
- Plain CSS. Pseudo-classes, media queries and custom properties all work as written. You keep your existing CSS knowledge and editor support.
- No runtime cost. The renaming happens during the build, so no JavaScript has to generate styles in the browser.
What you do not gain
Scoping applies to class names (and IDs and keyframe names). It does not isolate everything. Element selectors such as h1 { ... } are not renamed, so they still apply globally. Inherited properties such as font-family still flow down from parent elements. Specificity and source order also work as they always have.
Check your understanding
Exercise
Two components each define a .title class in their own .module.css files. Why don't their styles conflict, and where do you get the class name to put on the element?
Show answer
The bundler renames each .title to a different unique name at build time, so the two rules match different class names. The component gets the name from the imported styles object, for example className={styles.title}.