::selection
Example
::selection {
background: transparent;
}::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
::selectionignores. - Selection becomes invisible because its background matches the page color.
Key points
- Only text styles like
color,background-color,text-decorationandtext-shadoware 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.