简体   繁体   English

使用:: selection选择器更改所有子项的选择颜色

[英]Use ::selection selector to change selection color of all children

I have two div elements with any type of children in it (like input and span ) and I want to change the selection color of all child elements in my first div to red and in the second div to yellow. 我有两个div元素,其中包含任何类型的子元素(例如inputspan ),并且我想将第一个div中所有子元素的选择颜色更改为red ,将第二个div中所有子元素的选择颜色更改为黄色。

The following does not work: 以下工作:

#my-div-1::selection
{
  background-color: red;
}

#my-div-2::selection
{
  background-color: yellow;
}

I can not use the selector like this: 不能使用这样的选择器:

::selection
{
  background-color: yellow;
}

because this would overwrite the color of the second div and apply to the whole document. 因为这会覆盖第二个div的颜色并应用于整个文档。

You need to select all children elements where the selector should apply to. 您需要选择选择器将应用于的所有子元素 You can instead write the following: 您可以改写以下内容:

#my-div-1 *::selection
{
  background-color: red;
}

#my-div-2 *::selection
{
  background-color: yellow;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM