dialog - CSS 变量在 dialog::backdrop 中不起作用

标签 dialog modal-dialog css-variables

我正在尝试更改 dialog 的背景颜色元素的 backdrop使用自定义 CSS 属性,但不需要。这是 Chrome 中的错误还是有原因?

document.querySelector('dialog').showModal();
:root {
  --color-backdrop: red;
}

dialog::backdrop {
  background: var(--color-backdrop);
}
<dialog><p>This is a dialog. My backdrop should be red.</p></dialog>

最佳答案

spec声明以下关于 ::backdrop伪元素:

It does not inherit from any element and is not inherited from. No restrictions are made on what properties apply to this pseudo-element either.


并到 quote Xindorn Quan WHATWG 成员,关于 CSS 自定义属性:

CSS variables are propagated via inheritance into descendants, so if a pseudo-element doesn't inherit from anything, CSS variables which are not defined for the pseudo-element directly would have no effect on the pseudo-element.


最后,这是此类问题的一种解决方案:

document.querySelector('dialog').showModal();
::backdrop {
  --color-backdrop: red;
}

dialog::backdrop {
  background: var(--color-backdrop);
}
<dialog><p>This is a dialog. My backdrop should be red.</p></dialog>

它似乎对具有 ::backdrop 的多个模态很有用,作为组织他们的“根”的一种方式,可以这么说。

关于dialog - CSS 变量在 dialog::backdrop 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58818299/

相关文章:

java - 当复合组件放置在 PrimeFaces p :dialog 内时,不会调用 encodeAll 方法

c# - 在捕获异常时显示模态 Bootstrap

Css calc() 函数不适用于 sass 文件

wpf - 是否可以显示标题栏中没有图标的 wpf 窗口?

jQuery 对话框,将 href 作为图像源加载到对话框中

java - (Java Swing) 停止 actionPerformed 命令直到对话框返回

modal-dialog - Foundation 5 - 显示模态的顶部位置

ios - 如何在iOS中除状态栏区域外全屏显示模式?

css - 使用 calc() 正确添加 CSS 变量

css - 如何转换css变量的类型?