gpt4 book ai didi

影子 DOM 中的 CSS 优先级 <style>

转载 作者:行者123 更新时间:2023-11-28 11:00:05 27 4
gpt4 key购买 nike

关于 <style> 的 CSS 优先级规则是怎么说的?影子 DOM 中的标签?

我有一个元素 <component class="component"> , <head> 中包含的 CSS 文件与:

component {
display: inline-block;
}

和一个<style>在一些影子 DOM 中标记:

::slotted(.component) {
display: block;
}

如果我理解正确的话,第一条规则的特异性应该是0.0.1因为它有一个元素和第二个元素 0.1.1 的特异性因为它有一个伪元素和一个类。因此,第二个更具体,应该覆盖第一个。但这不会发生。在开发人员的控制台 (Chrome) 中,我看到了两条规则,但都没有被划掉,在“计算样式”面板中,我看到了“显示:内联 block ”。

评论中要求的更详细的示例:

<head>
<style>
/* "other-component" related styles: */
other-component {
display: inline-block;
}
</style>
</head>
<body>
<some-component>
#shadow-root:
<style>
slot[name=some-slot]::slotted(*) {
display: block; /* Only works with !important. */
}
</style>
<slot name="some-slot"></slot>
<!-- The actual ("light-dom") content: -->
<other-component slot="some-slot"></other-component>
</some-component>
</body>

最佳答案

此行为在 CSS Scoping Module Level 1 Draft §3.3 中定义:

When comparing two declarations that have different tree contexts, then for normal rules the declaration earlier in the shadow-including tree order [the first, global rule] wins, and for important rules the declaration coming later in the shadow-including tree [the second, ::slotted(*) rule] order wins.

Note: This is the opposite of how scoped styles work.

other worlds :

Styles that applied before distribution continue to apply after distribution.

关于影子 DOM 中的 CSS 优先级 &lt;style&gt;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54884345/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com