Sub topic 10
Using :global to target unscoped classes
This chapter assumes you can create and import a .module.css file, apply classes through the styles object, and read basic selectors. It builds on how CSS Modules generate unique class names.
Why you sometimes need to leave scope
CSS Modules rename every class selector in a module file at build time. That is what keeps .button in one file from colliding with .button in another. The same renaming causes a problem when the markup you want to style has class names you do not control.
Common cases:
- A third-party widget renders elements with fixed class names such as
.legacy-widget. - Legacy markup, or HTML from a CMS, uses plain global class names.
- A library adds state classes at runtime, such as
.is-open.
If you write .legacy-widget in a module, the build renames it to something like .Page_legacy-widget__x7Qa2. That name never appears in the DOM, so the rule matches nothing.
The :global function
Wrap a selector in :global(...) to tell CSS Modules not to rename it.
.page :global(.legacy-widget) {
margin: 0;
}Here .page is still local and gets a generated name. .legacy-widget stays exactly as written. The compiled rule matches a .legacy-widget element only when it sits inside the component's own .page element.
This is the main reason to prefer the form above. Anchoring the global class under a local class limits the rule to your component. Without the anchor, the rule affects the whole page.
:global(.legacy-widget) {
margin: 0;
}The second rule is valid, but it behaves like ordinary global CSS and can hit every .legacy-widget in the app.
How the selector is rewritten
The diagram shows how the build treats each part of the selector.
Using the global class in markup
The JSX side stays simple. The local class comes from styles. The global class is a plain string, because nothing renamed it.
import styles from './Page.module.css';
export function Page({ children }) {
return <div className={styles.page}>{children}</div>;
}The third-party widget rendered inside children already carries legacy-widget, so the rule applies without further changes.
The block form
To mark several selectors as global at once, use the bare :global keyword before a selector. Everything after it in that selector stays unscoped.
.page :global .legacy-widget .title {
font-weight: 600;
}Both .legacy-widget and .title stay global here. The function form is easier to read and less error-prone, so use it by default.
Choosing when to use it
Use :global sparingly. Each use gives up the guarantee that makes CSS Modules useful.
| Situation | Approach |
|---|---|
| Style your own component | Local class through `styles` |
| Override a library's inner element | `.local :global(.lib-class)` |
| Site-wide reset or base styles | A normal global stylesheet, not a module |
| Style a class you could rename | Rename it and use a local class |
Common mistakes
- Writing a bare
:global(.x)with no local anchor, which leaks the rule across the app. - Looking up a global class on the
stylesobject.styles['legacy-widget']isundefinedbecause that class was never renamed. - Forgetting that the rule still depends on the DOM structure. If the widget renders outside your
.pageelement, such as in a portal, the anchored selector will not match.
Exercise
A modal library renders its content in a portal attached to document.body with the class modal-body. Your rule .dialog :global(.modal-body) { padding: 0; } has no effect. Why, and what would fix it?
Show answer
The portal places .modal-body outside your .dialog element, so the descendant selector never matches. Options are to pass a local class to the library through a prop such as className, or to write a selector that does not depend on .dialog, like :global(.modal-body), accepting that it is global.