Local vs Global Scope in CSS Modules
You'll learn how CSS Modules keep your styles locally scoped by default and when to break out into global styles when you truly need them.
The Default: Local Scope
CSS Modules automatically scope every class name to the component that imports it. When you write .button in Button.module.css, it becomes something like .Button_button__a3x9z at runtime. This is local scope—your .button class won't clash with another .button somewhere else in your app.
Think of it like having your own private room: everything inside stays inside, no interference from other rooms.
When You Need Global Styles
Sometimes you genuinely need a style that applies everywhere—maybe a reset, a utility class, or a third-party library override. That's where :global() comes in:
/* Button.module.css */
.button {
/* This is local, gets hashed */
padding: 1rem;
}
:global(.disabled) {
/* This is truly global, stays as .disabled */
opacity: 0.5;
cursor: not-allowed;
}
Now .disabled won't be scoped—it applies globally across your entire app. Use this sparingly, only when local scope would break your design.
The Golden Rule
Default to local scope (the CSS Modules default). Only reach for :global() when you need a style that genuinely must be shared everywhere—like body resets, animation keyframes, or integrating with external libraries that expect specific class names.
Key Takeaway: CSS Modules default to local scope to prevent collisions; use :global() selectively for truly app-wide styles, but treat it as the exception, not the rule.