CSS Modules

Last updated: 2026-11-02

CSS Modules CSS Modules

CSS Modules turn an ordinary CSS file into locally scoped styles: at build time every class name is compiled into a unique identifier of the form fileName_className_hash, so components are naturally isolated and global style pollution disappears. In React you reference them through a styles.xxx object, which makes style isolation a zero-cost solution for medium and large projects. Nothing extra is required at runtime.

GroupEcosystem
KindPattern
SinceCommunity standard — natively supported by Vite / CRA / Next.js

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

/* No install needed — Vite / Create React App support it out of the box */
/* Button.module.css — the file name must end with .module.css */
.title { color: #0969da; }
.active { color: #1a7f37; }

// Button.jsx — class names are referenced through the styles object
import styles from './Button.module.css'

export default function Button() {
  return <button className={styles.title}>Button</button>
}
// styles.title compiles to 'Button_title_x7s2k2', globally unique and conflict-free

Returns:No return value — CSS Modules is a build-time solution; see the syntax and the example for how to use it.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

Simulation: the demo first shows the compiled hash class names of styles.card and styles.title, then renders a card below. Clicking the Activate title button appends the active hash class name to the title and turns it blue. In a real project these hash class names get their styles from a .module.css file.

💡 Key Points

💡 Tip The file name must end with .module.css (.module.scss and .module.less work the same way), otherwise the stylesheet is treated as global.
ℹ️ Info Write class names in camelCase in CSS (titleColor) and reference them as styles.titleColor, which avoids the styles['title-color'] bracket form.
⚠️ Warning When you concatenate className dynamically, separate several class names with a space — a missing space makes both classes fail.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q What is the difference between CSS Modules and plain CSS?
A Plain CSS class names are global, so identical names override each other. CSS Modules compiles class names at build time into unique identifiers of the form fileName_className_hash, so the styles only apply to the component that references them — isolation comes for free.
Q How do I write global styles?
A Wrap them in :global, for example :global(.ant-btn) { … }; or create a separate plain CSS file without the .module suffix and keep its class names global.
Q Does referencing a class that does not exist in styles throw an error?
A No error at runtime: styles.xxx is undefined and the element simply loses the class, which makes it hard to debug. TypeScript projects can generate a .d.ts file so spelling mistakes surface at compile time.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏