::selection

Term side of the Card.
Topic: Pseudo Elements
Unit title: ::selection
Categories: Pseudo-element
Explanation side of the Card

Explanation: The pseudo-element that applies styles to the portion of text that a user has highlighted or selected with the mouse or keyboard. It allows customization of the background color, text color and other properties of selected text, making it stand out visually when highlighted.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 62.
  • The safari browser supports this feature starting from version 1.1.
Availability: Limited

Example

Wrongcss
::selection {
  background: transparent;
}
Rightcss
::selection {
  background: #ffd84d;
  color: #111;
}

Changing the colors keeps selection visible, while a transparent background hides what the user selected.

Common mistakes

  • Setting a background image or gradient, which ::selection ignores.
  • Selection becomes invisible because its background matches the page color.

Key points

  • Only text styles like color, background-color, text-decoration and text-shadow are set.
  • Selected text keeps readable contrast with the selection background.
  • Selection is turned off with user-select, not hidden with a transparent ::selection.

Interview questions

Can I disable text selection with ::selection?

No. Use user-select: none to stop selection; a transparent ::selection only hides it and confuses users.

Which properties work with ::selection?

Only a few: color, background-color, text-decoration and its longhands, and text-shadow. Box and layout properties are ignored.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy