Local Scope by Default
You'll learn how CSS Modules automatically scope class names locally to prevent style collisions across your application.
The Global Namespace Problem
In traditional CSS, every class name you write is global. If you create a .button class in one component and another developer creates a .button class elsewhere, they'll conflict—the last one loaded wins. This makes maintaining large applications fragile and unpredictable.
How CSS Modules Solve This
CSS Modules flip the default behavior: every class name is local to its module unless you explicitly make it global. When you write .button in a CSS Module, the build tool transforms it into something unique like .button_a8f3k2 at compile time.
Think of it like apartment buildings versus one giant shared room. In traditional CSS, everyone's furniture is in the same space, causing chaos. CSS Modules give each component its own apartment—you can name your couch "sofa" without worrying that your neighbor also has something called "sofa."
What This Means in Practice
When you write:
/* Button.module.css */
.button {
background: blue;
}
CSS Modules automatically generates a unique class name. Your component imports this transformed name, so there's zero risk of collision with another component's .button class. Each module creates its own isolated namespace.
You don't need to write long, prefixed class names like .myApp__componentName__button anymore. Just write .button, and the tooling handles uniqueness for you.
The Freedom of Local Scope
This local-by-default approach means you can use simple, semantic class names without fear. Multiple components can have .header, .title, or .container classes, and they'll never interfere with each other.
Key Takeaway: CSS Modules make every class name locally scoped by default, automatically generating unique identifiers to eliminate style collisions without requiring manual naming conventions.