Global Styles in CSS Modules
What you'll learn: How to selectively break out of local scoping in CSS Modules when you need global styles or must override third-party components.
The Problem with Pure Local Scope
By now you know that CSS Modules scope everything locally by default—every class name gets transformed into something unique. But sometimes you need truly global styles: resetting browser defaults, styling third-party components that inject their own markup, or creating intentionally global utility classes.
The :global() Escape Hatch
CSS Modules provides the :global() pseudo-class to opt specific selectors out of local scoping. Anything wrapped in :global() stays exactly as written—no hashing, no transformation.
Basic syntax:
/* styles.module.css */
.myComponent {
padding: 1rem;
}
/* This class gets hashed (local) */
.localHelper {
color: blue;
}
/* This class stays as-is (global) */
:global(.override-third-party) {
font-size: 1.2rem;
}
When to use it:
- Third-party overrides: Styling components from libraries that inject their own class names
- Global resets: Body or HTML element styles that need page-wide effect
- Integration points: When your component needs to style children it doesn't directly control
Think of it like this: CSS Modules is a gated community where everything gets a unique address. :global() is your VIP pass to write on the public streets outside.
Mixing Local and Global
You can combine both in one selector:
.myComponent :global(.external-widget) {
/* .myComponent is local, .external-widget stays global */
margin-top: 1rem;
}
Key Takeaway: Use :global() sparingly to break out of CSS Modules' local scope only when you truly need global reach—for resets, third-party overrides, or intentional cross-component styles.