--- title: '::selection' slug: 'Web/CSS/::selection' tags: - CSS - Layout translation_of: 'Web/CSS/::selection' ---
{{ CSSRef() }}
::selection CSS伪元素应用于文档中被用户高亮的部分(比如使用鼠标或其他选择设备选中的部分)。
::selection {
background-color: cyan;
}
只有一小部分CSS属性可以用于::selection 选择器:
要特别注意的是,{{ cssxref("background-image") }} 会如同其他属性一样被忽略。
/* Legacy Firefox syntax (version 61 and below) */
::-moz-selection
{{CSSSyntax}}
This text has special styles when you highlight it. <p>Also try selecting text in this paragraph.</p>
::-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;
}
{{EmbedLiveSample('示例')}}
| Specification | Status | Comment |
|---|---|---|
| {{ SpecName('CSS4 Pseudo-Elements', '#selectordef-selection', '::selection')}} | {{ Spec2('CSS4 Pseudo-Elements')}} | Initial specification |
::selection CSS伪元素选择器是CSS第3级选择器的草案,但是在被推荐使用前就被废弃。它现在在第4级伪元素选择器草案中。
{{Compat("css.selectors.selection")}}