无法使用CSS选择器选择,如何设置样式使”Launch”文字颜色为红色?

huangapple go评论65阅读模式
英文:

Unable to select <slot> using CSS Selector, how to style it to make "Launch" text color red?

问题

根据上面的截图,我正在尝试为<slot>元素和插槽内的内容添加样式,以便在单击时显示“Launch”。我如何使用CSS选择器来为插槽添加文本颜色以显示“Launch”?我尝试使用下面的选择器来应用样式,但对我来说没有起作用。

sl-tab::part(base)::slotted(*) {
  color: red;
}
英文:

无法使用CSS选择器选择<slot>,如何设置样式使”Launch”文字颜色为红色?

As per above screenshot, I am trying to style web component <slot>, and content inside slot reveal/pointing to "Launch" when clicked. How can I style slot to apply text colour to "Launch". I am struggling to select slot using css selector. Any help/suggestions would be helpful.

I already tried to apply style using below selector but did not work for me.

sl-tab::part(base)::slotted(*) {
color: red;
}

答案1

得分: 0

翻译结果如下:

未将插槽内容移动到 shadowDOM,因此无法从 shadowDOM 中进行样式设置

插槽内容会被反射到 shadowDOM

插槽(lightDOM)内容的样式由 lightDOM 所在的容器/级别控制,此处使用全局 CSS 进行样式设置(请参见 H1 和 span 的样式设置)。

::slotted(span) 只能样式化 lightDOM 内容的外观,无法样式化更深的 DOM 层级(只有 span2 被样式化)。

使用主机元素上的 [attribute] 来样式化插槽文本(请参见 launched)。

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->
customElements.define("component-with-slot", class extends HTMLElement {
  constructor() {
    super().attachShadow({ mode: "open" }).innerHTML =
      `<style>
            *::slotted(span) { padding: 2em; } 
            :host([launched])  { color: red; font-weight: bold; }
          </style>` +
      `<slot></slot>`;
  }
});

<!-- 语言:lang-css -->
/* 全局 CSS */
h1 {
  border: 5px solid blue;
}
span {
  border: 4px dashed red;
}

<!-- 语言:lang-html -->
<component-with-slot>
  <h1>I am slotted <span>span1</span></h1>
  <span>span2</span>
</component-with-slot>

<component-with-slot launched>
  Launch happened
</component-with-slot>

<!-- 结束代码片段 -->

有关更详细的信息,请参阅:https://stackoverflow.com/questions/61626493/slotted-css-selector-for-nested-children-in-shadowdom-slot/61631668#61631668

英文:

Slotted content is not moved to shadowDOM, therefor you can not style it from shadowDOM

Slotted content is reflected to shadowDOM

Slotted (lightDOM) content is styled by the container/level the lightDOM is in, in this case global CSS (see H1 and span styling)

::slotted(span) can only style the skin of lightDOM content, not DOM levels deeper (only span2 is styled)

Style slotted text with an [attribute] on the host element (see launched)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

customElements.define(&quot;component-with-slot&quot;,class extends HTMLElement{
  constructor(){
    super().attachShadow({mode:&quot;open&quot;}).innerHTML = 
     `&lt;style&gt;
        *::slotted(span) { padding:2em } 
        :host([launched])  { color:red; font-weight:bold }
      &lt;/style&gt;` +
     `&lt;slot&gt;&lt;/slot&gt;`;
  }
});

<!-- language: lang-css -->

/* global CSS */
h1 {
  border:5px solid blue
}
span {
  border:4px dashed red;
}

<!-- language: lang-html -->

&lt;component-with-slot&gt;
  &lt;h1&gt;I am slotted &lt;span&gt;span1&lt;/span&gt;&lt;/h1&gt;
  &lt;span&gt;span2&lt;/span&gt;
&lt;/component-with-slot&gt;

&lt;component-with-slot launched&gt;
  Launch happened
&lt;/component-with-slot&gt;

<!-- end snippet -->

For very detailed info see: https://stackoverflow.com/questions/61626493/slotted-css-selector-for-nested-children-in-shadowdom-slot/61631668#61631668

答案2

得分: 0

我找到了另一种方法来为"Launch"添加样式:

sl-tab[active]::part(base) {
  color: red;
}
英文:

I figured out how to style Launch with another way using:

sl-tab[active]::part(base) {
  color: red;
}

huangapple
  • 本文由 发表于 2023年5月29日 20:23:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定