Definition and Usage
The ::selection pseudo-element is a selector that allows you to style text selected by the user by dragging with a mouse or other input methods (also known as highlighted text).
Remember!
The ::selection pseudo-element does not apply styles to the entire element. Instead, it applies styles only to the portion of text that the user explicitly selects by dragging with a mouse or using the keyboard.
The styles are automatically applied to the selected text as soon as the user selects it. When the selection is cleared, the styles disappear as well.
Syntax
::selection {
/* ... */
}
Basic Example
<p>
Drag your mouse to select some of the text in this paragraph
and see how the styling changes for the selected portion.
</p>
p::selection {
background-color: yellow;
color: red;
}
Drag your mouse to select some of the text in this paragraph and see how the styling changes for the selected portion.
Only Certain Style Properties Are Allowed
The ::selection pseudo-element selector only allows a limited set of style properties so that styles can be applied dynamically while the user drags to select text without affecting the surrounding layout. This is because affecting the surrounding layout could negatively impact the user experience or accessibility.
The specification explicitly specifies which properties can be applied to the ::selection pseudo-element selector, but the properties supported vary by browser.
The following properties are supported by most major browsers, so they can be safely applied to the ::selection pseudo-element selector:
background-colorcolortext-shadow
Try the following example to see for yourself:
<p>
Drag your mouse to select some of the text in this paragraph
and see how the styling changes for the selected portion.
</p>
p::selection {
background-color: yellow;
color: red;
text-shadow: 2px 2px 1px black;
}
Drag your mouse to select some of the text in this paragraph and see how the styling changes for the selected portion.
Accessibility Considerations
When styling the ::selection pseudo-element selector, you should always consider the user's perspective. In other words, styling purely for aesthetic reasons can sometimes make things uncomfortable for users. In particular, low contrast can cause difficulties for users with color vision deficiencies or low vision. For example, using a light text color on a light background reduces readability and should be avoided.
The color contrast between selected text and its background should be sufficient for users with color vision deficiencies or low vision. Refer to WCAG 2.1 to maintain appropriate color contrast.
Browser Compatibility
| Desktop | ||||
|---|---|---|---|---|
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
::selection
|
1 | 12 | 62 | 1.1 |
Last updated:
| Mobile | |||
|---|---|---|---|
| Selector |
Mobile Chrome
|
Mobile Firefox
|
Safari
|
::selection
|
18 | 62 | No |
Specifications
| Specification | |
|---|---|
::selection
|
CSS Pseudo-Elements Module Level 4 #selectordef-selection |
References
See Also
- CSS ::before Pseudo-Element Selector
- CSS ::after Pseudo-Element Selector
- CSS ::marker Pseudo-Element: Select Automatically Generated List Markers
- CSS ::placeholder Pseudo-Element for Styling Placeholder Text
- CSS pointer-events Property: Controlling Pointer Interactions
- CSS cursor Property: Styling the Mouse Cursor