:has-slotted

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimentell: Dies ist eine experimentelle Technologie
Überprüfen Sie die Browser-Kompatibilitätstabelle sorgfältig vor der Verwendung auf produktiven Webseiten.

Die :has-slotted CSS Pseudoklasse greift, wenn der Inhalt eines <slot>-Elements nicht leer ist oder keinen Standardwert verwendet (siehe Verwendung von Templates und Slots für weitere Informationen).

Hinweis:Selbst ein einzelner Leerzeichen-Textknoten reicht aus, damit:has-slotted angewendet wird.

Diese Pseudoklasse funktioniert nur, wenn sie innerhalb von CSS verwendet wird, das sich im Shadow DOM befindet.

css
/* Selects the content of a <slot> element that has content that is not default  */
:has-slotted {
  color: green;
}

/* Selects the content of a <slot> element that has no content or default  */
:not(:has-slotted) {
  color: red;
}

Syntax

css
:has-slotted {
  /* ... */
}

Beispiele

Dieses Beispiel enthält zwei <slot>-Elemente, von denen eines mit Inhalt belegt wurde und das andere nicht.

HTML

html
<p>
  <template shadowrootmode="open">
    <style>
      :has-slotted {
        color: rebeccapurple;
      }
    </style>
    <slot name="one">Placeholder 1</slot>
    <slot name="two">Placeholder 2</slot>
  </template>
  <span slot="one">Slotted content</span>
</p>

Ergebnis

Das <slot>-Element, dem Inhalt zugewiesen wurde, entspricht der Pseudoklasse :has-slotted und hat den color-Wert rebeccapurple erhalten.

Spezifikationen

Specification
CSS Scoping Module Level 1
# the-has-slotted-pseudo

Browser-Kompatibilität

BCD tables only load in the browser

Siehe auch