Scope CSS selectors with a custom class name.
npm install css-scopingCSS Scoping is a utility function that automatically prepends a given class name to a given CSS string, making it easier to encapsulate styles and avoid conflicts across external styles.
``bash`
npm install css-scoping
): The raw CSS code to be scoped.
- blockClass (string): The class name to prepend to selectors for scoping (e.g., .my-component).
- globalSelectors (string[], optional): Selectors that should not be scoped (default: ['html', 'body', ':root']).$3
`js
import scopeCss from './index.js';const css =
;const scoped = scopeCss(css, '.my-block');
console.log(scoped);
`Output:
`
.my-block .button {
color: red;
}
html, body {
margin: 0;
}
@keyframes fade {
from { opacity: 0; }
to { opacity: 1; }
}
``