Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:host — styling a Shadow DOM host

The :host pseudo-class lets a stylesheet inside Shadow DOM select its host element. Outside a shadow-tree context, it matches nothing.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

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

:host selects a shadow tree's host: the element to which the shadow root is attached. Place the rule in that tree's stylesheet; in a document stylesheet, :host matches nothing.

The selector targets the host, not its internal elements. Its specificity is (0,1,0). The variant :host(.active) additionally tests the host's class and has specificity (0,2,0); its argument is a compound selector without combinators, not a selector list.

In the example, the stylesheet inside the shadow root sets display and padding on host. The result reads the host's computed styles, not those of the span element.

Examples

<!doctype html>
<meta charset="utf-8">
<title>:host example</title>
<div id="host"></div>
<pre id="result"></pre>
<script>
  const host = document.getElementById('host');
  const root = host.attachShadow({ mode: 'open' });
  root.innerHTML = `
    <style>
      :host { display: block; padding: 8px; }
    </style>
    <span>Shadow content</span>
  `;
  const style = getComputedStyle(host);
  document.getElementById('result').textContent =
    `display: ${style.display}\npadding: ${style.paddingTop}`;
</script>

Result

display: block
padding: 8px

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources