31

我正在试验::selectionCSS3 中的伪元素。在 Firefox 中它可以工作并且看起来很棒。我的网站有深蓝色背景。

我设置了选择,使其在 FF 中看起来像这样。

在此处输入图像描述

但在 chrome 中,相同的测试看起来像这样。似乎 chrome 将选择解释为半透明,结果颜色很讨厌。

在此处输入图像描述

有谁知道是否有可能让 chrome 的行为与 Firefox 相同。

这里是我的css供参考:

p::-moz-selection { background:#FFFF7D; color:#032764; }
p::-webkit-selection { background:#FFFF7D; color:#032764; }
p::selection { background:#FFFF7D; color:#032764; }

谢谢

4

2 回答 2

47

由于某种原因,Chrome 强制它是半透明的。但是,您可以通过设置backgroundusing rgba 来解决此问题。我已将 alpha 值设置为仅比 1 小 0.01。现场示例:http: //jsfiddle.net/tw16/m8End/

p::-moz-selection {
    background:rgba(255, 255, 125, 0.99);
    color:#032764;
}
p::-webkit-selection {
    background:rgba(255, 255, 125, 0.99);
    color:#032764;
}
p::selection {
    background:rgba(255, 255, 125, 0.99);
    color:#032764;
}
于 2011-08-28T23:08:54.897 回答
9

正如Steven Lu 在对tw16 答案的评论中指出的那样,不透明度阈值为。255/256

                 计算

换句话说,0.996会工作,但0.997不会。

让我们看看它的实际效果:

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

::-moz-selection
{
  background: #F80;
  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>

正如您在 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>
    <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>

在 Firefox 中,似乎无法覆盖图像上的蓝色选择颜色。

于 2015-04-02T13:16:06.227 回答