Spec-Zone.ru › CSS

`::selection` CSS pseudo-element

Baseline Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

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 pseudo-element follows a special inheritance model common to all highlight pseudo-elements. For more details on how this inheritance works, see the Highlight pseudo-elements inheritance section.

Try it

p::selection {
  color: red;
  background-color: yellow;
}
<p>
  Select a fragment of this paragraph, to see how its appearance is affected.
</p>

Allowable properties

Only certain CSS properties can be used with ::selection:

  • color
  • background-color
  • text-decoration and its associated properties
  • text-shadow
  • -webkit-text-stroke-color, -webkit-text-fill-color and -webkit-text-stroke-width

In particular, background-image is ignored.

Syntax

::selection {
  /* ... */
}

Accessibility

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.)

  • WebAIM: Color Contrast Checker
  • MDN Understanding WCAG, Guideline 1.4 explanations
  • [Understanding Success Criterion 1.4.3 W3C Understanding WCAG 2.0](https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html)

Examples

HTML

This text has special styles when you highlight it.
<p>Also try selecting text in this paragraph.</p>

CSS

::-moz-selection {
  color: gold;
  background-color: red;
}

p::-moz-selection {
  color: white;
  background-color: blue;
}
/* 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;
}

Result

Specifications

Specification
CSS Pseudo-Elements Module Level 4
# selectordef-selection

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on iOS Samsung Internet WebView Android WebView on iOS
selection
1
12
62
1
9.5
1.1
18
62
4
14
No
1.0
37
No
text-decoration
105
105
No
91
No
105
No
72
No
20.0
105
No

See also

  • pointer-events - control which events are active on the element

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/::selection

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API