CSS selection

Question

How can I set the selection’s color in CSS?

Answer

By using the ::selection pseudo-element property.
the syntax is:

::selection {
  background: yellow;
}

Of course you can set what should be it’s set on(in this example I’m going to use h1 elements):

h1::selection {
  background: green;
}

You can even disable selections by setting background to none or unset
You can set other properties, for example box-shadow and more

1 Like

Just a warning, though, this is not supported by all browsers.

I would check https://caniuse.com/#search=%3A%3Aselection to make sure that it is supported by browsers you may be targeting.

1 Like