The interesting question about styling web components is how to define styles that don't leak out of the Shadow DOM — not how to let global styles in, though custom properties can do that. Each approach below trades convenience against code organization, request overhead, and browser support.
Styles as a variable
When the template is assembled in JavaScript (attractive for template literals and easy data interpolation), the styles have to be reachable from JavaScript too. The value of style in the snippet below typically comes from another template literal.
const template = `
<style>${styles}</style>
<div class="${class}">
<h2>${title}</h2>
${content}
</div>
`;
const style = `
:host {
background: white;
}
h2 {
font: 900 1.5rem/1.1 -system-ui, sans-serif;
}
`;
This works, but it makes for a big messy block of code stuck somewhere inside the component class.
Templates holding their own styles
Another option is a <template> with a <style> block inside it. Keeping HTML in HTML is appealing, but populating the template means manual work such as shadowRoot.querySelector("#title-hook").innerHTML = myData.title;, which is not a convenient template. The template also has to live somewhere in the HTML, with no obvious home, so the CSS merely moves from one awkward location to another.
<template id="card-template">
<style>
:host {
background: white;
}
h2 {
font: 900 1.5rem/1.1 -system-ui, sans-serif;
}
</style>
<div id="card-hook">
<h2 id="title-hook"></h2>
<p id="desc-hook"></p>
</div>
</template>
Pointing at a stylesheet file
CSS can be kept in a CSS file in a rough sense:
<template id="card-template">
<style>
@import "/css/components/card.css";
</style>
<div id="card-hook">
<h2 id="title-hook"></h2>
<p id="desc-hook"></p>
</div>
</template>
The <link rel="import" type="css" href=""> form is deprecated. What remains is @import, which costs an extra HTTP request and has a reputation as a performance hit — Steven Lambert measured it at half a second slower. Fetching the sheet and injecting it instead doesn't obviously improve on that:
class MyComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
fetch('/css/components/card.css')
.then(response => response.text())
.then(data => {
let node = document.createElement('style');
node.innerHTML = data;
document.body.appendChild(node);
});
}
// ...
}
That approach risks a Flash-of-Unstyled-Web-Component.
The ::part route
::part has gained some traction (see the explainer) and lets you style from a global stylesheet, touching only the inside of the Shadow DOM:
const template = `
<div part="card">
<h2>${title}</h2>
${content}
</div>
`;
my-card::part(card) {
background: black;
color: white;
}
Browser support is thin, though, and part selectors reach only the element they're attached to. Every DOM node needs a part name and its own styling — which undercuts the point of the Shadow DOM, an isolated environment where loose selectors like h2 { } are safe.
Native CSS modules and constructing sheets
Native CSS modules, if they land, look like the most useful development for this problem:
import styles from './styles.css';
class MyElement extends HTMLElement {
constructor() {
this.attachShadow({mode: open});
this.shadowRoot.adoptedStyleSheets = [styles];
}
}
Whether that brings a performance gain is unclear; it seems a wash against @import. The syntax is clearer, and it keeps you writing JavaScript while working in JavaScript.
Constructable Stylesheets also help share a stylesheet across multiple components, but the CSS modules approach can likely do the same, since the stylesheet is already a variable by that point.



