Day 45: the specificity of ::slotted() content

When you pass an element to a web component through a <slot>, you can select that element using the ::slotted() pseudo-element and apply additional styles.

Let’s take the following component. There’s a paragraph in the shadow DOM and another paragraph coming from the light DOM, passed through a <slot>, and there’s a global style turning the background color of paragraphs aqua.

p {
background-color: aqua;
}
class SlotComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});

const content = document.createElement('div')
content.innerHTML = `
<p>not slotted</p>
<slot></slot>
`

this.shadowRoot.appendChild(content)
}
}

customElements.define('slot-component', SlotComponent);
<slot-component>
<p>slotted</p>
</slot-component>

slotted

The global styles only apply to the slotted paragraph. We’ve already learned why in “Day 10: global styles and web components”.

If you add styles to the shadow DOM, you can see how these styles only apply to the paragraph inside the shadow DOM, but not to the slotted paragraph.

const styles = document.createElement('style')
styles.innerHTML = `
p {
background-color: salmon;
}
`

this.shadowRoot.appendChild(styles)

slotted

If you want to style slotted content from within the component, you can use the ::slotted() pseudo-element.

const styles = document.createElement('style')
styles.innerHTML = `
p {
background-color: salmon;
}

::slotted(p) {
background-color: red;
}
`

this.shadowRoot.appendChild(styles)

slotted

As you can see, the background color didn’t change. That’s because by slotting content you’re not moving it from the light DOM to the shadow DOM. The nodes physically stay where they are, they’re just reflected inside the web component. This also means that global document styles still apply. By using ::slotted() we can add additional styles, but by default these styles have lower specificity than global document styles.
That changes if we add !important to the mix.

const styles = document.createElement('style')
styles.innerHTML = `
p {
background-color: salmon;
}

::slotted(p) {
background-color: red !important;
}
`

this.shadowRoot.appendChild(styles)

slotted


This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović

When you pass an element to a web component through a <slot>, you can select that element using the ::slotted() pseudo-element and apply additional styles.

Let's take the following component. There's a paragraph in the shadow DOM and another paragraph coming from the light DOM, passed through a <slot>, and there's a global style turning the background color of paragraphs aqua.

p {
background-color: aqua;
}
class SlotComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});

const content = document.createElement('div')
content.innerHTML = `
<p>not slotted</p>
<slot></slot>
`

this.shadowRoot.appendChild(content)
}
}

customElements.define('slot-component', SlotComponent);
<slot-component>
<p>slotted</p>
</slot-component>

slotted

The global styles only apply to the slotted paragraph. We've already learned why in “Day 10: global styles and web components”.

If you add styles to the shadow DOM, you can see how these styles only apply to the paragraph inside the shadow DOM, but not to the slotted paragraph.

const styles = document.createElement('style')
styles.innerHTML = `
p {
background-color: salmon;
}
`

this.shadowRoot.appendChild(styles)

slotted

If you want to style slotted content from within the component, you can use the ::slotted() pseudo-element.

const styles = document.createElement('style')
styles.innerHTML = `
p {
background-color: salmon;
}

::slotted(p) {
background-color: red;
}
`

this.shadowRoot.appendChild(styles)

slotted

As you can see, the background color didn't change. That's because by slotting content you're not moving it from the light DOM to the shadow DOM. The nodes physically stay where they are, they're just reflected inside the web component. This also means that global document styles still apply. By using ::slotted() we can add additional styles, but by default these styles have lower specificity than global document styles.
That changes if we add !important to the mix.

const styles = document.createElement('style')
styles.innerHTML = `
p {
background-color: salmon;
}

::slotted(p) {
background-color: red !important;
}
`

this.shadowRoot.appendChild(styles)

slotted


This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2022-11-25T09:38:54+00:00) Day 45: the specificity of ::slotted() content. Retrieved from https://www.scien.cx/2022/11/25/day-45-the-specificity-of-slotted-content/

MLA
" » Day 45: the specificity of ::slotted() content." Manuel Matuzović | Sciencx - Friday November 25, 2022, https://www.scien.cx/2022/11/25/day-45-the-specificity-of-slotted-content/
HARVARD
Manuel Matuzović | Sciencx Friday November 25, 2022 » Day 45: the specificity of ::slotted() content., viewed ,<https://www.scien.cx/2022/11/25/day-45-the-specificity-of-slotted-content/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 45: the specificity of ::slotted() content. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/25/day-45-the-specificity-of-slotted-content/
CHICAGO
" » Day 45: the specificity of ::slotted() content." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2022/11/25/day-45-the-specificity-of-slotted-content/
IEEE
" » Day 45: the specificity of ::slotted() content." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2022/11/25/day-45-the-specificity-of-slotted-content/. [Accessed: ]
rf:citation
» Day 45: the specificity of ::slotted() content | Manuel Matuzović | Sciencx | https://www.scien.cx/2022/11/25/day-45-the-specificity-of-slotted-content/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.