2015-09-28 58 views
4

因此,为了覆盖浏览器的默认选择颜色,通过:: selection伪元素进​​行文本选择。但对我来说,白色作为选择背景颜色似乎被禁止使用灰色,不透明的颜色。Webkit:Css文本选择样式不能使背景变白?

::selection  { color: black; background: white; } 
 
::-moz-selection { color: black; background: white; } 
 

 
body { 
 
    background: black; 
 
    color: white; 
 
}
So selecting me is supposed to invert the text and background colors. But the selected text background color is not white, but grey.

为什么这个创建一个完美的白色背景?相反,它显示了一个灰色的背景(#989898)

Css selection does not accept white background

也可参阅https://jsfiddle.net/duk3/hzodm1sh/

+0

你明白这个结果是什么浏览器? – chiapa

+0

Chrome和Safari发生这种情况 - 目前版本仍然存在 – Romainpetit

回答

3

好了,所以跟随着g this question我找到了答案:

由于某些原因,Chrome强制它是半透明的。不过,你可以通过使用rgba设置背景来解决这个问题。我已经设置 的Alpha值只是0.01小于1

::selection { color: black; background: rgba(255, 255, 255, 0.996); 
 
; } 
 

 
body { 
 
    background: black; 
 
    color: white; 
 
}
Selecting me DOES invert the text and background colors. Well, almost, but it looks like perfect white.

0

这个片段你应该尝试这样的 -

::-moz-selection { 
 
    color:black; 
 
    background: white; 
 
} 
 

 
::selection { 
 
    color:black; 
 
    background: white; 
 
} 
 
body { 
 
    background: black; 
 
    color: white; 
 
}
So selecting me is supposed to invert the text and background colors.

+0

您可以添加截图吗? – Romainpetit

1

也许这是一个浏览器特定的问题。

试试这个:

Fiddle

::selection { 
 
    color: black; 
 
    background: white; 
 
} 
 
body { 
 
    background: black; 
 
    color: white; 
 
} 
 
::-moz-selection { 
 
    color: black; 
 
    background: white; 
 
}
So selecting me is supposed to invert the text and background colors.

下面是结果在打印:

...

+1

您正在链接到红色背景颜色选择示例。我也看到它红色,我的问题涉及白色具体 – Romainpetit

+0

更正@Romainpetit – chiapa

+0

好吧,我编辑moz前缀属性,所以我们现在使用相同的代码。你看到真正的白色或灰色作为选择背景吗? – Romainpetit