It is a misconception slotted content is MOVED to slots in shadowDOM
It is NOT!;
It remains invisible in lightDOM and is REFLECTED to its SLOT in shadowDOM
That means you can apply styles after content has been slotted (mouseover in the code below)
Or.. to style UNnamed slots, you style the UNnamed content in lightDOM:
For a really deep dive explanation see: ::slotted CSS selector for nested children in shadowDOM slot
customElements.define("my-element", class extends HTMLElement {
connectedCallback() {
let template = document.getElementById(this.nodeName);
this.attachShadow({
mode: 'open'
}).appendChild(template.content.cloneNode(true));
}
})
my-element div {
background: lightcoral;
padding: 1em;
margin-top:.5em;
}
my-element div:hover {
background: lightgreen;
}
h1{
background:lightblue;
color:black;
margin:0;
}
h1:hover {
color: red;
}
<template id=MY-ELEMENT>
<style>
:host {
display: block;
padding:.5em;
background:green;
}
::slotted(*){
color:white;
}
div{
border:1px dashed black;
}
</style>
<div>
<slot name=title></slot>
<slot></slot>
</div>
</template>
<my-element>
<div>Custom Elements Rule!</div>
<h1 slot=title>Hello World!</h1>
<div>What a wonderfull day!</div>
</my-element>
Note! how all unnamed content goes into the (one) unnamed SLOT