React CSS Modules & Local Scope
In traditional frontend development, CSS rules are applied globally. If you define a `.button` class in two different stylesheets, they will clash, causing unexpected styling bugs. CSS Modules solve this by scoping your CSS strictly to the component that imports it.
The Power of Hashing
When a bundler (like Webpack or Vite) encounters a file named .module.css, it parses the CSS and transforms every class name into a unique, hashed string. For example, .header might become _Header_header__3k9A. This completely eliminates global collisions without requiring complex naming conventions like BEM.
Importing as an Object
Because the class names change at build time, you cannot type them manually in your JSX. Instead, you import the CSS file as a JavaScript object:import styles from './MyComponent.module.css';
Now, styles contains a mapping from your original class names to the hashed ones. You apply them dynamically: className={styles.header}.
Best Practices: camelCase
While CSS traditionally uses kebab-case (.my-button), it is highly recommended to use camelCase (.myButton) inside CSS Modules. This allows you to use standard JS dot notation (styles.myButton) instead of bracket notation (styles['my-button']).
View Advanced Techniques+
Composition: You can use `composes` inside CSS modules to inherit styles from other local classes or even other files. `composes: primary from './Button.module.css';`
Global Scope: If you intentionally want a class to remain un-hashed (for example, to target a third-party library child element), wrap it in `:global(.class-name)`.
