:host

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

We'd love to hear more about your role and the company you work for
Please help us by answering a few questions.

Die :host CSS Pseudoklasse wählt den Schattenwirt des Shadow DOM aus, der das CSS enthält, in dem sie verwendet wird — mit anderen Worten, dies ermöglicht es, ein benutzerdefiniertes Element von innerhalb seines Shadow DOM auszuwählen.

Hinweis: Dies hat keine Wirkung, wenn es außerhalb eines Shadow DOM verwendet wird.

Probieren Sie es aus

/* This CSS is being applied inside the shadow DOM. */

:host {
  background-color: aqua;
}
<h1 id="shadow-dom-host"></h1>
const shadowDom = init();

// add a <span> element in the shadow DOM
const span = document.createElement("span");
span.textContent = "Inside shadow DOM";
shadowDom.appendChild(span);

// attach shadow DOM to the #shadow-dom-host element
function init() {
  const host = document.getElementById("shadow-dom-host");
  const shadowDom = host.attachShadow({ mode: "open" });

  const cssTab = document.querySelector("#css-output");
  const shadowStyle = document.createElement("style");
  shadowStyle.textContent = cssTab.textContent;
  shadowDom.appendChild(shadowStyle);

  cssTab.addEventListener("change", () => {
    shadowStyle.textContent = cssTab.textContent;
  });
  return shadowDom;
}
css
/* Selects a shadow root host */
:host {
  font-weight: bold;
}

Syntax

css
:host {
  /* ... */
}

Beispiele

Den Schattenwirt stylen

Die folgenden Ausschnitte stammen aus unserem host-selectors Beispiel (sehen Sie es auch live).

In diesem Beispiel haben wir ein einfaches benutzerdefiniertes Element — <context-span> — das Sie um Text herum platzieren können:

html
<h1>
  Host selectors <a href="#"><context-span>example</context-span></a>
</h1>

Im Konstruktor des Elements erstellen wir style und span Elemente, füllen das span mit dem Inhalt des benutzerdefinierten Elements und das style Element mit einigen CSS-Regeln:

js
const style = document.createElement("style");
const span = document.createElement("span");
span.textContent = this.textContent;

const shadowRoot = this.attachShadow({ mode: "open" });
shadowRoot.appendChild(style);
shadowRoot.appendChild(span);

style.textContent =
  "span:hover { text-decoration: underline; }" +
  ":host-context(h1) { font-style: italic; }" +
  ':host-context(h1):after { content: " - no links in headers!" }' +
  ":host-context(article, aside) { color: gray; }" +
  ":host(.footer) { color : red; }" +
  ":host { background: rgb(0 0 0 / 10%); padding: 2px 5px; }";

Die Regel :host { background: rgb(0 0 0 / 10%); padding: 2px 5px; } stylt alle Instanzen des <context-span> Elements (in diesem Fall der Schattenwirt) im Dokument.

Spezifikationen

Specification
CSS Scoping Module Level 1
# host-selector

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
:host

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch