CSS Reference Pseudo-class

::selection 4j49g

The ::selection CSS pseudo-element represents a portion of the document that is highlighted by the . For example, a portion of a page that is selected by the using the mouse or any other pointing device. 5q212p

selection-default
In most browsers, selected content has a blue background color by default. Using ::selection, you can change that to match the overall style or theme of the page.

Only a subset of all CSS properties can be used to style a ::selection, namely the CSS text-shadow properties.

You can also use the background shorthand can basically only be used to specify a background color.

The ::selection pseudo-element was drafted for CSS Selectors Level 3 but was removed before it reached the Candidate Recommendation status, and it is currently not part of any CSS module on the standards track. However, it is still implemented in most browsers, and is likely to remain so.

Examples 3e5p5z

The following example sets the foreground and background colors of any selected portion of a page:

::selection {
    background-color: #222;
    color: white;
}
                

The following example sets the foreground and background colors of text highlighted in a blockquote:

blockquote::selection {
    background-color: #aaa;
    color: white;
}
                

Live Demo 2x4j2k

Have a look at the live demo:

View this demo on the Codrops Playground

Browser k5t66

The ::selection pseudo-element is ed in Chrome, Safari, Opera, and Internet Explorer 9+.

Firefox s it prefixed with the -moz- prefix, in the form: ::-moz-selection.

Using text-shadow in ::selection is only ed in Chrome, Safari, Opera, nd Firefox.

Written by

Last updated June 3, 2020 at 12:33 pm by Mary Lou

Do you have a suggestion, question or want to contribute? Submit an issue.