html - Firefox 和 Chrome 坚持使用 blue::selection 背景

标签 html css google-chrome firefox selection

我无法让 Chrome (41) 和 Firefox (36) 将特定颜色应用到整个选择背景,尽管指定了橙色背景,但有些区域仍然是蓝色。

考虑以下示例:

::selection
{
  background: rgba(255, 127, 0, 0.996);
  color: white;
}

::-moz-selection
{
  background: #F80;
  color: white;
}

img::selection
{
  background: rgba(255, 127, 0, 0.8);
  color: white;
}
<p>The domestic cat is a small, usually furry, domesticated, and carnivorous mammal. They are often called a housecat when kept as an indoor pet, or simply a cat when there is no need to distinguish them from other felids and felines. Cats are often valued by humans for companionship.</p>
    <img src="http://placekitten.com/g/75/300">
    <img src="http://placekitten.com/g/300/300">
    <img src="http://placekitten.com/g/150/300">
<p>A Melvin, Michigan woman was brutally attacked by a stray cat and shocking footage of the mauling has already gained a million views on YouTube.</p>
<p>The woman, who identified herself to reporters only as Maxx, endured the horrific attack November 30 but only recently realized it had been caught on her home surveillance camera.</p>
<p>The attack left her face swollen and infected and the cat named Buddy dead as officials were forced to test it for rabies.</p>

当按下 Ctrl+A 时,Chrome(和 Opera)将如下所示:

chrome or opera

Firefox 看起来像这样:

firefox

令人惊讶的是,Internet Explorer (11) 是正确的:

internet explorer

在上面的 Chrome 和 Firefox 示例中,有没有办法使整个选择成为橙色或白色/透明?

最佳答案

Chrome 和家庭(像小兄弟 Opera,没有检查表弟 Safari)的问题是 ::selection 不适用于通过在 a 之前添加文本节点创建的隐式 block 包装器 block 元素。即使没有图像也可以看到:

::selection
{
  background: rgba(255, 127, 0, 0.996);
  color: white;
}
<p>Chrome loves you.</p>
Chrome hates you.
<p>Chrome is tsundere.</p>

按 Ctrl+A 后在 Chrome 上的结果如下所示:

tsundere Chrome

这正是那些图像所发生的事情。看这个例子:

::selection
{
  background: rgba(255, 127, 0, 0.996);
  color: white;
}

::-moz-selection
{
  background: #F80;
  color: white;
}

img::selection
{
  background: rgba(255, 127, 0, 0.8);
  color: white;
}
<p>The domestic cat is a small, usually furry, domesticated, and carnivorous mammal. They are often called a housecat when kept as an indoor pet, or simply a cat when there is no need to distinguish them from other felids and felines. Cats are often valued by humans for companionship.</p>
<p>
    <img src="http://placekitten.com/g/75/300">
    <img src="http://placekitten.com/g/300/300">
    <img src="http://placekitten.com/g/150/300">
</p>
<p>A Melvin, Michigan woman was brutally attacked by a stray cat and shocking footage of the mauling has already gained a million views on YouTube.</p>
<p>The woman, who identified herself to reporters only as Maxx, endured the horrific attack November 30 but only recently realized it had been caught on her home surveillance camera.</p>
<p>The attack left her face swollen and infected and the cat named Buddy dead as officials were forced to test it for rabies.</p>

像这样将 img 包装在 p 中之后,我们在 Chrome 上有这个:

chrome

Firefox 仍然是个谜。如果不是因为我之前对 CSS 的可能性感到困惑(并且浏览器有时会有像 -webkit-appearance 这样的东西,我会选择“不可能的 CSS”我)。如果做不到这一点,应该可以用 javascript 以某种方式做到这一点。使用 CSS filter 可以为图像着色,这是最难的部分。

关于html - Firefox 和 Chrome 坚持使用 blue::selection 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29438095/

相关文章:

javascript - 将 CSS 样式从一个元素复制到单独文档中的新元素

html - css中定位div,一左一右

html - DIV float :right with no text wrapping

xml - 如果文件名中缺少 '.xml' 后缀,为什么 Chrome 会尝试下载/保存从 localhost 上的 gatsby 提供的 xml 文件,而不是在浏览器中显示?

html - Safari :visited border color automatically on div?

javascript - formvalidation.io 外部提交按钮

css - IE9不支持显示: inline-flex at all?吗

css - 当窗口高度减小时,margin-top 百分比不会改变

google-chrome - 在Google Chrome浏览器中使用 “i18n-content”进行国际化

html - 为什么当我使用 Tab 键时,Chrome 浏览器会在我的图像链接周围显示一个奇怪形状的突出显示?