CSS Portal

::slotted() CSS Pseudo Element

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The ::slotted() pseudo-element in CSS is used in conjunction with the slot attribute to style elements that are passed into a shadow DOM from the light DOM. In other words, it allows authors of a web component to target and apply styles to children that are slotted into the shadow DOM, without breaking encapsulation. This pseudo-class is a critical tool for maintaining design consistency within shadow DOM components while still allowing flexibility for content insertion.

When using ::slotted(), you must provide a simple selector inside the parentheses, such as a type selector, class selector, or ID selector. Complex selectors like combinators or descendant selectors are not allowed. For example, to style all p elements passed into a slot, you would write:

::slotted(p) {
  color: blue;
  font-weight: bold;
}

This ensures that any p elements inserted into the slot adopt the specified styles, regardless of where they originate in the light DOM. Similarly, you can target elements by class:

::slotted(.highlight) {
  background-color: yellow;
  border-radius: 5px;
}

It’s important to note that ::slotted() applies styles only to the slotted element itself, not to its descendants. For instance, if a slotted element contains nested span elements, you cannot style the nested span elements using ::slotted() directly. This restriction preserves the encapsulation that the shadow DOM provides.

Another practical aspect of ::slotted() is that it allows component developers to provide default styling for expected light DOM content while still giving authors of the light DOM flexibility to override these styles outside the shadow DOM if needed. This balances encapsulation and flexibility, which is one of the core advantages of using Web Components.

Example with a shadow DOM component:

<my-card>
  <p>Slotted paragraph</p>
  <button class="cta">Click me</button>
</my-card>

<script>
  const template = document.createElement('template');
  template.innerHTML = `
    <style>
      ::slotted(p) {
        color: red;
        font-size: 18px;
      }
      ::slotted(.cta) {
        padding: 10px 20px;
        background-color: green;
        color: white;
      }
    </style>
    <slot></slot>
  `;

  class MyCard extends HTMLElement {
    constructor() {
      super();
      const shadow = this.attachShadow({ mode: 'open' });
      shadow.appendChild(template.content.cloneNode(true));
    }
  }

  customElements.define('my-card', MyCard);
</script>

In this example, any p or element with the class cta inserted into the slot will adopt the specified styles defined by ::slotted(), demonstrating how this pseudo-class allows controlled styling of projected content.

Syntax

::slotted(<compound-selector>) {
  /* ... */
}

Example

<my-card>
<h2 slot="title">Hello World</h2>
<p>This is some slotted content that will be styled from inside the shadow root.</p>
</my-card>

<template id="card-template">
<style>
/* Targets any &lt;h2&gt; that is placed in a slot */
::slotted(h2) {
color: #2a9d8f;
font-family: sans-serif;
text-transform: uppercase;
margin-bottom: 0;
}

/* Targets any paragraph placed in the default slot */
::slotted(p) {
color: #666;
line-height: 1.6;
font-style: italic;
}

/* Internal wrapper styling (not slotted) */
.wrapper {
border: 2px solid #e76f51;
padding: 20px;
border-radius: 8px;
max-width: 300px;
}
</style>
<div class="wrapper">
<slot name="title"></slot>
<slot></slot>
</div>
</template>

<script>
customElements.define('my-card', class extends HTMLElement {
constructor() {
super();
const template = document.getElementById('card-template');
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(template.content.cloneNode(true));
}
});
</script>
/* CSS in HTML section */

Browser Support

The following information will show you the current browser support for the CSS ::slotted() pseudo element. Hover over a browser icon to see the version that first introduced support for this CSS psuedo element.

This psuedo element is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!