Slotted content is not moved to shadowDOM, therefor you can not style it from shadowDOM
Slotted content is reflected to shadowDOM
Slotted (lightDOM) content is styled by the container/level the lightDOM is in, in this case global CSS (see H1 and span styling)
::slotted(span)
can only style the skin of lightDOM content, not DOM levels deeper (only span2 is styled)
Style slotted text with an [attribute]
on the host element (see launched
)
customElements.define("component-with-slot",class extends HTMLElement{
constructor(){
super().attachShadow({mode:"open"}).innerHTML =
`<style>
*::slotted(span) { padding:2em }
:host([launched]) { color:red; font-weight:bold }
</style>` +
`<slot></slot>`;
}
});
/* global CSS */
h1 {
border:5px solid blue
}
span {
border:4px dashed red;
}
<component-with-slot>
<h1>I am slotted <span>span1</span></h1>
<span>span2</span>
</component-with-slot>
<component-with-slot launched>
Launch happened
</component-with-slot>
For very detailed info see: ::slotted CSS selector for nested children in shadowDOM slot