Shadow DOM <style> 中的 CSS 优先级

标签 css shadow-dom css-specificity

CSS 优先规则对 <style> 有何规定? Shadow DOM 中的标签?

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

component {
    display: inline-block;
}

和一个<style>一些 Shadow DOM 中的标签:

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

如果我理解正确的话,第一条规则的特殊性应该是 0.0.1因为它有一个元素,第二个元素为 0.1.1因为它有一个伪元素和一个类。因此,第二个更具体,应该覆盖第一个。但这并没有发生。在开发人员控制台(Chrome)中,我看到了这两条规则,但它们都没有被划掉,并且在“计算样式”面板中,我看到了“display: inline-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.

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

相关文章:

ios - iOS5 上的 CSS3 无限旋转动画

javascript - 通过 CSS 更改 JQuery Mobile 的字体和颜色

javascript - 替换折叠固定标题中的图像(滚动) - javascript

javascript - 具有 Bootstrap 的响应式全宽图像网格

html - 带有 !important::CSS 的多个类选择器

css - 计算 :not() pseudo-class 的 CSS 选择器特异性

web-component - 自定义元素上的 createElement 破坏模板

javascript - 如何让 Shadow Dom 与 LIst 一起工作?

c# - Selenium C# : Not able to click on an element within #shadow-root (closed) which appears when user mouse over

html - CSS 样式优先级未按预期工作