为什么不能使用body::selection
,当我想要任何可以高亮显示为我想要的颜色时,任何包括p img li h1
例子在这里
现在我希望所有的东西都高亮显示为红色,但我使用的是body::selection
,它永远行不通
最佳答案
如果要将选择背景应用于所有元素,请省略类型选择器:
::selection {
background: red;
}
为此,添加 ::-moz-selection
使其在 Firefox 中也能正常工作:
::-moz-selection {
background: red;
}
::selection {
background: red;
}
从未决定您为任何元素 E
为 E::selection
设置的样式应该如何传播到 E
的后代。有更深入的讨论 in the www-style mailing list .也正是由于这个原因,::selection
已完全从 CSS3 UI 的最新 LC 修订版中删除;见this section ,它说:
The ::selection pseudo-element has been dropped since it was dropped from Selectors after testing found interoperability problems and further details to explore/define.
我最好的猜测是浏览器(至少是 Firefox)不会对后代元素应用相同的规则。因此,如果将伪元素应用于 body
,则只有 body
文本具有自定义选择背景;嵌套在其中的所有内容都没有选择背景。
关于css - 使用 body::selection,我想自定义高亮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9046042/