大网数据广告活动
Web 标准与前端开发

`:has-slotted` CSS pseudo-class

· 52CE 我爱测

The **`:has-slotted`** CSS pseudo-class matches when the content of a slot element is not empty or not using the default value (see Using templates and slots for more information).

The :has-slotted CSS pseudo-class matches when the content of a slot element is not empty or not using the default value (see Using templates and slots for more information).

[!NOTE] Even a single whitespace text node is sufficient to make :has-slotted apply.

This only works when used inside CSS placed within a shadow DOM.

/* 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

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

Examples

This example has two <slot> elements, one of which has been assigned some content and the other has not.

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>

Result

The <slot> element that has been assigned content matched the :has-slotted pseudo-class and has the color value of rebeccapurple applied.

examples

Specifications

Browser compatibility

See also

  • HTML template element
  • HTML slot element
  • ::slotted

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。