Change Text Selection Color and Background in CSS

Last Updated on January 26, 2021 by Roshan Parihar

In this tutorial, learn how to change text selection color and background using CSS. The short answer is: use the CSS selector ::selection in combination with ::-moz-selection that add color and background to the text selected.

However, the CSS selector ::-moz-selection is required to make color and background visible in the Firefox browser on selection. Let’s find out with the examples given below:-

How to Change Text Selection Color in CSS

To change the text selection color in CSS, you have to use the CSS property color in the ::selection and ::-moz-selection CSS selector.

See the example given below to apply the color to the selected paragraph content.

Test it Live

Output

Select the below text to see the effect

This is a paragraph content to check the selection color.

When you select the text content given in the example above, you will get the above text content that appears green in color. You can change the color as per the requirement of your website and match it with your website color.

Set Text Background to Display on Select in CSS

To set the background color to display on text selection, you have to use the CSS property background in the ::selection and ::-moz-selection CSS selector.

See the example given below that added a yellow color to the background:-

Test it Live

Output

Select the below text to see the effect

Check the background color by selecting this paragraph content.

You have to select the text content given above to check the background color. It changes the background color to the specified yellow color.

Add Both Color and Background to the Selected Text Using CSS

In addition to the above examples, you can add both color and background to the display on text selection. you have to use both CSS properties background and color together in the ::selection and ::-moz-selection CSS selector.

Test it Live

Output

Select the below text to see the effect

Select this paragraph content to check the color and background.

The above example contains both the background and the color to display when someone selects the text content. You can select the text content given in the example above to get the background and color on the selection.

You may also like to read