翻译正在进行中。

::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;
}
/* Make selected text gold on a red background */
::selection {
  color: gold;
  background-color: red;
} 

/* Make selected text in a paragraph white on a blue background */
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 AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic support
Experimental
Chrome Full support 1Edge Full support YesFirefox 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 ? Chrome Android ? Edge Mobile Full support YesFirefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

文档标签和贡献者

标签: 
最后编辑者: mdnwebdocs-bot,