::selection-设置选择框样式-css伪对象选择器【select选择器默认值】 ypxx.net

::selection

版本:CSS3

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

语法:

E::selection{sRules}

设置对象被选择时的CSS样式。::selection能定义被选择时的background-color,color及text-shadow(IE11尚不支持定义该属性)。

::selection {background-color: cyan;}

允许属性

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

  • color
  • background-color
  • cursor
  • caret-color
  • outline
  • text-decoration
  • text-emphasis-color
  • text-shadow

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

浏览器支持

IE9+以及新版浏览器都支持::selection
IE8以及早期IE版本不支持::selection

例子

//HTMLthis text has special styles when you highlight it.

also try selecting text in this paragraph.

//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;}this text has special styles when you highlight it.

also try selecting text in this paragraph.

.demo1 ::-moz-selection{color: gold; background-color: red;}.demo1 p::-moz-selection{color: white; background-color: blue;}.demo1 ::selection{color: gold; background-color: red;}.demo1 p::selection{color: white; background-color: blue;}