This does not work because it is not supported in IE
IE does not support inherit when styling a pseudo-element ::selection :
The style properties in the :: selection pseudo-element do not support the value "inherit".
:: Pseudo-element of choice ( https://msdn.microsoft.com/en-us/library/jj127349(v=vs.85).aspx )
You can do the following to get a similar result
The desired behavior can be achieved without using inherit using the :not() pseudo- inherit , although it will require some small changes to the HTML. The principle of this is that:
- You override the
background-color for all elements with ::selection {background: #c6c6c6;} - Move the
override class to the p elements themselves - You override
color for all elements except those that have a .override class with :not(.override)::selection{color: #000;} .p0{color: blue;} to show that the change is working
I do not think that this behavior can be obtained without changing the HTML, since the :not() class pseudo-class accepts a simple selector .
::selection { background: #c6c6c6; } :not(.override)::selection { color: #000; } .p0 { color: blue; } .p1 { color: red; } .p2 { color: green; }
<div> <p class="p0">Heading 1</p> <div class="override"> <p class="p1 override">Sub heading 1</p> <p class="p2 override">Sub heading 1</p> </div> </div>
JS script example
Alternatively, you can achieve this without modifying the HTML
If all the classes you want to override start with p , you can use the attribute selector [foo^="bar"] :
::selection { background: #c6c6c6; } :not([class^=p])::selection { color: #000; } .t1 { color: blue; } .p1 { color: red; } .p2 { color: green; }
<div> <p class="t1">Heading 1</p> <div class="override"> <p class="p1">Sub heading 1</p> <p class="p2">Sub heading 1</p> </div> </div>
JS script example
source share