Sub topic 15
Common pitfalls and organizing styles
This chapter assumes you can write React components with props and JSX, and that you know CSS selectors and specificity. It also assumes you have already imported a .module.css file and applied classes through the styles object. Here you will meet the mistakes that show up most often and a layout that keeps them away.
Pitfall 1: Unscoped element selectors
CSS Modules only rewrite class names and ids. A rule such as p { margin: 0; } stays global and affects every paragraph in the app. Most tooling rejects a file with no local class in a selector. This is "pure mode" in postcss-modules, and Next.js enforces the same rule.
/* Leaks into every <a> in the app */
a {
color: #2563eb;
}
/* Scoped: only links inside .card */
.card a {
color: #2563eb;
}Prefer a class on the element itself. If you must style descendants, anchor the selector to a local class, as .card a does above.
Pitfall 2: Relying on generated class names
The generated name, such as Button_button__x7Qa2, changes with build settings and file paths. Do not hard-code it in tests, in global CSS, or in analytics selectors. If external code needs a stable hook, add a data-testid or another data- attribute.
Pitfall 3: Undefined classes
Accessing a class that does not exist returns undefined. React then renders no class and shows no error.
// A typo: the CSS file defines .button, not .buton
<button className={styles.buton}>Save</button>With dynamic lookups like styles[variant], a bad variant value fails the same way. Defend against it with a fallback, or restrict variant to a union type in TypeScript.
Pitfall 4: Using `composes` the wrong way
composes has three constraints:
- It must be the first rule in the block.
- It works only with a single class selector.
- It cannot compose from a class that is declared later in the same file in some setups.
Composition also does not control cascade order. If two composed classes set the same property, the winner depends on the order of the CSS in the final bundle, not the order in composes. Keep composed classes from setting the same properties.
Pitfall 5: Overusing `:global`
Every :global escape reintroduces the collision risk you adopted modules to avoid. Limit it to targets you do not control, such as a third-party widget, and always nest it under a local class, as in .page :global(.legacy-widget).
Organizing styles in a project
Keep each stylesheet next to its component and give it the component's name. One file per component keeps the scope small and makes unused styles easy to spot when you delete a component.
src/
components/
Button/
Button.jsx
Button.module.css
Card/
Card.jsx
Card.module.css
styles/
global.css
tokens.cssPut resets, fonts and design tokens in plain global files under styles/. Define tokens as custom properties on :root, and read them from modules with var(--color-primary). This gives you shared values without sharing class names.
Practical rules
| Situation | Do this |
|---|---|
| Styling an element type | Add a local class instead of a bare element selector |
| Several variants | Base class plus variant classes, joined with `clsx` |
| Shared look across files | Share a token, or use `composes ... from` sparingly |
| Third-party markup | `.local :global(.vendor-class)` |
| Runtime values | Set a custom property through `style` |
| Test hooks | Use `data-testid`, not generated names |
Exercise
A teammate writes button { background: red; } in Form.module.css and finds that every button in the app turns red. Why did this happen, and how do you fix it?
Show answer
CSS Modules only localize class and id names. The bare button selector is not rewritten, so it applies globally. Fix it by giving the element a local class, such as .submit { background: red; }, and using className={styles.submit}.
Exercise
A test looks for .Button_button__x7Qa2 and breaks after a build config change. What should the test use?
Show answer
A stable attribute such as data-testid, or a query by role and accessible name. Generated class names are not part of the public contract.