The ::selection
CSS pseudo-element applies styles to the part of a document that has been highlighted by the user (such as clicking and dragging the mouse across text).
The ::selection
CSS pseudo-element applies styles to the part of a document that has been highlighted by the user (such as clicking and dragging the mouse across text).
Only certain CSS properties can be used with ::selection
:
color
background-color
text-decoration
and its associated propertiestext-shadow
-webkit-text-stroke-color
, -webkit-text-fill-color
and -webkit-text-stroke-width
In particular, background-image
is ignored.
::selection { /* ... */ }
This text has special styles when you highlight it. <p>Also try selecting text in this paragraph.</p>
/* Make selected text gold on a red background */ ::selection { color: gold; background-color: red; } /* Make selected text in a paragraph white on a blue background */ p::selection { color: white; background-color: blue; }
Don't override selected text styles for purely aesthetic reasons — users can customize them to suit their needs. For people experiencing cognitive concerns or who are less technologically literate, unexpected changes to selection styles may hurt their understanding of the functionality.
If overridden, it is important to ensure that the contrast ratio between the text and background colors of the selection is high enough that people experiencing low vision conditions can read it.
Color contrast ratio is found by comparing the luminosity of the selected text and the selected text background colors. To meet current Web Content Accessibility Guidelines (WCAG), text content must have a contrast ratio of 4.5:1, or 3:1 for larger text such as headings. (WCAG defines large text as between 18.66px
and 24px
and bold, or 24px
or larger.)
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
::selection |
1 | 12 | 621 | 9 | 9.5 | 1.1 | 37 | 18 | 624 | 14 | No | 1.0 |
pointer-events
- control which events are active on the element
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/::selection