Skip to content

Instantly share code, notes, and snippets.

@MarkTiedemann
Created February 14, 2025 15:43
Show Gist options
  • Save MarkTiedemann/78713fd56896c2e5ead70387f0460505 to your computer and use it in GitHub Desktop.
Save MarkTiedemann/78713fd56896c2e5ead70387f0460505 to your computer and use it in GitHub Desktop.
const cssUrl = "/your.css";
const cssRequest = await fetch(cssUrl);
if (!cssRequest.ok) {
throw new Error(`Failed to load '${cssUrl}' (status: ${cssRequest.status})`);
}
const cssText = await cssRequest.text();
const styleSheet = new CSSStyleSheet();
await styleSheet.replace(cssText);
document.adoptedStyleSheets = [styleSheet, ...document.adoptedStyleSheets];
// CSS is ready to be used now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment