Sub topic 3
Creating and importing a .module.css file
This chapter assumes you know basic React (components, props, JSX), CSS selectors and specificity, ES module imports, and how to run a project built with a bundler such as Vite, webpack or Next.js.
The naming convention
A CSS Module is an ordinary CSS file with .module.css in its name. The suffix tells the bundler to treat the file differently from a global stylesheet. A plain Button.css is injected as-is, and its class names stay global. Button.module.css is processed so its class names become local to whoever imports it.
By convention the file sits next to its component and shares its name:
src/
components/
Button.jsx
Button.module.cssWriting the stylesheet
Write the CSS as you normally would. Nothing in the syntax changes.
.button {
padding: 0.5rem 1rem;
border: 0;
border-radius: 4px;
background: #2563eb;
color: #fff;
cursor: pointer;
}
.button:hover {
background: #1d4ed8;
}The name .button is scoped to any component that imports this file. Another file can also define .button without any conflict.
Importing the file
Import the stylesheet as a default import. The bundler turns it into a JavaScript object, which this tutorial calls the styles object. Each key is a class name from your CSS file. Each value is the unique class name the build generated for it.
import styles from './Button.module.css';
export function Button({ children }) {
return <button className={styles.button}>{children}</button>;
}Here, styles.button is a string such as Button_button__x7Fq2. The exact value depends on your build configuration. React puts that string in the element's class attribute, and the browser matches it against the rewritten selector in the CSS.
The import also has a side effect. When the component's module is loaded, the bundler makes sure the processed CSS reaches the page, either as a <style> tag or as an extracted stylesheet file.
What happens at build time
The bundler reads your CSS, renames each local class, and emits two outputs. One is the rewritten stylesheet. The other is the styles object that maps your original names to the new ones. Because both come from the same step, they always agree.
Common mistakes at this stage
- Wrong file suffix. Naming the file
Button.cssand importingstylesfrom it gives youundefinedfor every key, because no mapping object is created. Rename it toButton.module.css. - Named imports of unknown classes.
styles.buton(a typo) does not throw. It evaluates toundefined, so React renders no class at all. If a style is missing, logstylesto see the real keys. - Side-effect-only import. Writing
import './Button.module.css';without a binding loads the CSS but gives you no way to reference the classes.
Exercise
Exercise
You create Card.css containing .card { padding: 1rem; } and write import styles from './Card.css'; <div className={styles.card} />. The padding is not applied. What is wrong, and how do you fix it?
Show answer
The file does not end in .module.css, so the bundler does not treat it as a CSS Module. No styles mapping is created, and styles.card is undefined. Rename the file to Card.module.css and update the import path to ./Card.module.css.