: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.

:host()CSS擬似クラス関数で、その中で使われている CSS を含むシャドウ DOM のシャドウホストを選択します(従ってカスタム要素をそのシャドウ DOM 内部から選択できます)。ただし、関数の引数として与えられたセレクターがシャドウホストと一致した場合のみです。 :host() はシャドウ DOM の外で使用しても効果はありません。

この最も明白な使用法は、特定のカスタム要素インスタンスにのみクラス名を付け、関数の引数として関連するクラスセレクターを指定することです。特定の祖先の内部にあるカスタム要素のインスタンスのみを選択するために、子孫セレクター式でこれを使用することはできません。それは :host-context() の仕事です。

メモ: 他にも、 :is():not() のような関数型擬似クラスは、引数として複合セレクターのリストを受け入れますが、 :host() は単一の複合セレクターを受け入れます。さらに、 :is():not() が引数の詳細度しか考慮しないのに対して、 :host() の詳細度は擬似クラスの詳細度および引数の詳細度の両方です。

試してみましょう

css
/* セレクターの引数に一致する場合のみ、
   シャドウルートのホストを選択 */
:host(.special-custom-element) {
  font-weight: bold;
}

構文

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

シャドウホストの選択的なスタイル付け

以下のスニペットは、 host-selectors example (ライブでも確認) から引用したものです。

この例では、テキストを囲むことができるシンプルなカスタム要素 <context-span> を用意しています。

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

要素のコンストラクター内で stylespan の各要素を作成し、 span にカスタム要素の内容を入れ、 style 要素にいくつかの CSS ルールを入れています。

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; }";

ルール :host(.footer) { color : red; } は、文書内の <context-span> 要素(この例ではシャドウホスト)のインスタンスで footer クラスがあるものにスタイルを設定します。このルールを使って <footer> 内の要素のインスタンスに特殊な色を設定しています。

仕様書

Specification
CSS Scoping Module Level 1
# host-selector

ブラウザーの互換性

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
See implementation notes.

関連情報