CSS ::selection Selector

CSS ::selection selector allows you to add styles to that portion of an element which is selected by the user. This selection happens when you double-click on the text or drag the mouse across the text.

For most of the browsers, the selected content has a blue background color and white text color. However, we can very easily get rid of these default colors and replace them with the ones that we like.

The following example sets a yellow background color and black text color when you select any portion of the page:

Example:

::selection{
   background: yellow;
   color: black;
}

You can also apply the selection colors to a particular element such as paragraph, div, list, input, textarea, etc.

See this example:

Example:

p::selection{
    background: green;
    color: white;
}
div::selection{
    background: yellow;
    color: black;
}

The background color and the font color are not the only things that you can apply to the selected portion of an element.

You can also apply the text-shadow property to the selected text.

See this example:

Example:

::selection{
    text-shadow: 1px 1px 2px red;
}

Please note that only the below three properties can be used with the ::selection selector:


CSS Syntax

The syntax of the ::selection selector is as follows:

::selection{
   CSS Styles;
}

Browser Support

The number in each cell of the table specifies the first version of the browser that fully supports the ::selection selector

Selector
::selection4.09.062.03.110.1

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.