Sub topic 4
Applying classes through the styles object
This chapter assumes you know basic React (components, props, JSX), CSS selectors, and ES module imports. It also assumes your project already builds .module.css files, as Vite, Next.js and Create React App do by default.
The import is an object
A CSS Modules file does not add styles by name. You import it, and the import gives you an object. Each key is a class name you wrote in the CSS file. Each value is the unique class name the build tool generated for it.
Start with a stylesheet:
.button {
padding: 0.5rem 1rem;
border: 0;
border-radius: 4px;
background: #2563eb;
color: #fff;
cursor: pointer;
}
.button:hover {
background: #1d4ed8;
}Save it 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 is conventionally named styles. The expression styles.button is a string such as Button_button__x7Fq2. React puts that string in the class attribute, and the browser matches it against the rewritten selector in the CSS.
What happens at build time
The bundler rewrites the selector and builds the object in one step, so the two always match. If you rename .button in the CSS and not in the JSX, styles.button becomes undefined.
Where the mistakes happen
The first mistake is writing a plain string. This does not work:
<button className="button">Save</button>The CSS now contains .Button_button__x7Fq2, so a literal button class matches nothing. Always read the name from styles.
The second is a typo or a missing class. JavaScript does not warn you about a missing key.
<button className={styles.buton}>Save</button>This renders class="undefined" in some setups and no class in others. Check the element in your browser's dev tools. If the class looks wrong, compare the key against the CSS file.
The third is expecting element or attribute selectors to be renamed. Only class names (and IDs and keyframe names, depending on the tool) are rewritten. A rule like button { margin: 0 } in a module file stays global, so prefer a class.
Inspecting the object
When something does not apply, log the import:
import styles from './Button.module.css';
console.log(styles);
// { button: 'Button_button__x7Fq2' }If the key you want is missing, the problem is in the CSS file. Common causes are a class that is only used inside a selector you did not write, or a name containing a hyphen, which is covered in the next chapter.
Summary of the pattern
- Write classes in a
*.module.cssfile next to the component. - Import the file as
styles. - Pass
styles.<name>toclassName.
Exercise
A component renders <div className="card"> and its Card.module.css defines .card { padding: 1rem; }. The padding never appears. What is wrong and how do you fix it?
Show answer
The CSS Modules build renames .card to a generated name such as Card_card__a1B2c, so the literal string "card" matches nothing. Import the file with import styles from './Card.module.css'; and use className={styles.card}.