REACT STYLING MASTERY /// SCOPED CSS /// CSS MODULES /// REACT STYLING MASTERY /// SCOPED CSS /// CSS MODULES /// REACT STYLING MASTERY /// SCOPED CSS /// CSS MODULES ///

React CSS Modules

Say goodbye to global class name collisions. Learn to write CSS that automatically scopes itself to individual React components.

Button.jsx
1 / 11
12345
📦

Tutor:Welcome to React CSS Modules. Normally in React, if you import a standard CSS file, the classes become globally scoped. This means a '.button' class in one file can accidentally override a '.button' class in another file.


Skill Matrix

UNLOCK NODES BY MASTERING SCOPE.

Concept: Import syntax

To use CSS Modules, the file must end in .module.css and must be imported as an object in your JavaScript.

System Check

How do you correctly import a CSS module in React?


Community Holo-Net

Showcase Your Architecture

ACTIVE

Built a complex layout without a single class collision? Share your architecture.

React CSS Modules & Local Scope

Author

Pascual Vila

Frontend Instructor // Code Syllabus

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)`.

React CSS Modules Glossary

CSS Module

A CSS file in which all class names and animation names are scoped locally by default.

snippet.js

Hashing

The process bundlers use to turn a generic class name like '.button' into a unique string like '.Button_button__xyz'.

snippet.js

Import Object

When importing a CSS module, it is treated as a JS object containing original classes mapped to their hashed versions.

snippet.js

Dynamic className

Applying the CSS Module styles in React using curly braces to evaluate the JavaScript object property.

snippet.js

Template Literals

A JavaScript feature used to combine multiple CSS module classes together in a single className prop.

snippet.js

:global()

A pseudo-class used inside a CSS Module to explicitly declare that a selector should NOT be hashed locally.

snippet.js