You get this effect using the pseudo-element ::selection
.
The pseudo-element
has support for most browsers including IE-9
, but on today’s date 24/10/18
there is no support for the IOS Safari
nor for the Opera Mini
as can be seen on the site Can I Use.
It would look like this:
::selection {
background-color: red;
color: lightgreen;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae porta purus, in tincidunt dui. Ut ut neque neque. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Ut porttitor convallis purus sed porttitor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac velit turpis. Integer sed dui quam. Suspendisse laoreet aliquam velit, sed interdum sapien. Donec id nisl non libero vehicula malesuada rutrum auctor ex. </p>
Obs: For <=61 versions of firefox, you need to use the following syntax:
::-moz-selection
Reference: https://developer.mozilla.org/en-US/docs/Web/CSS/:Selection
That’s all
::selection
– LeAndrade
But only with this pseudo-element will the effects be applied in all browsers?
– João Pedro Schmitz
From: Chrome 4.0, IE 9.0, Firefox 2.0, Safari 3.1, Opera 10.1. Since for Firefox it is necessary to use the prefix -Moz-
– LeAndrade
Maybe this link can help you: https://css-tricks.com/overriding-the-default-text-selection-color-with-css/
– Mike Otharan
@Leandrade Show mano! Thanks for the warning, it is of great help!
– João Pedro Schmitz
@Mikeotharan very good link content! Thank you for sharing!
– João Pedro Schmitz