add a CSS to document or specified target using constructed stylesheet with fallback
adoptedStyleSheets API
bash
npm i add-css-constructed
`
Usage
$3
`javascript
import { addCSS } from 'add-css-constructed';
// Add CSS to document
const style = addCSS('body { color: red; }');
// Add CSS to shadow root
const shadowRoot = element.attachShadow({ mode: 'open' });
addCSS('div { padding: 10px; }', shadowRoot);
`
$3
`javascript
const addCSS = require('add-css-constructed');
addCSS('.container { margin: 0 auto; }');
`
$3
`html
`
API
$3
Adds CSS styles to the specified target.
Parameters:
- css_code (string): CSS code to inject
- target (Document|ShadowRoot|null): Target element, defaults to document
Returns:
- CSSStyleSheet|HTMLStyleElement: The created stylesheet or style element
Throws:
- Error: When no DOM is detected and no target provided
$3
Removes CSS style sheet from document or specified target.
Returns:
- boolean: True if the removal operation is successful
$3
Checks if DOM environment is available.
Returns:
- boolean: True if DOM is available
$3
Checks if adoptedStyleSheets API is supported.
Returns:
- boolean: True if adoptedStyleSheets is supported
Browser Support
- Modern browsers with adoptedStyleSheets support
- All browsers with traditional