::selection

翻译不完整。 请帮助我们翻译这篇文章!

::selection CSS伪元素应用于文档中被用户高亮的部分(比如使用鼠标或其他选择设备选中的部分)。

::selection {
  background-color: cyan;
}

允许属性

只有一小部分CSS属性可以用于::selection 选择器:

要特别注意的是,background-image 会如同其他属性一样被忽略。

语法

/* Legacy Firefox syntax (version 61 and below) */
::-moz-selection

::selection

示例

HTML

This text has special styles when you highlight it.
<p>Also try selecting text in this paragraph.</p>

CSS

::-moz-selection {
  color: gold;
  background-color: red;
}

p::-moz-selection {
  color: white;
  background-color: blue;
}
/* 选中的文本是红色背景,金黄色的字体 */
::selection {
  color: gold;
  background-color: red;
} 

/*选中的是蓝色背景,白色的字体的段落*/
p::selection {
  color: white;
  background-color: blue;
}

结果

规范

Specification Status Comment
CSS Pseudo-Elements Level 4
::selection
Working Draft Initial specification

::selection CSS伪元素选择器是CSS第3级选择器的草案,但是在被推荐使用前就被废弃。它现在在第4级伪元素选择器草案中。

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
::selectionChrome Full support 1Edge Full support 12Firefox Full support 62
Full support 62
Full support 1
Prefixed
Prefixed Implemented with the vendor prefix: -moz-
IE Full support 9Opera Full support 9.5Safari Full support 1.1WebView Android Full support 37Chrome Android Full support 18Firefox Android Full support 62
Full support 62
Full support 4
Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Opera Android Full support 14Safari iOS Full support 1Samsung Internet Android Full support 1.0

Legend

Full support  
Full support
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.