# ::selection

> The `::selection` CSS pseudo-element styles the portion of text that has been selected by the user with the mouse or keyboard.

## How do I change the text highlight color with ::selection?

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.

- Topic: Pseudo Elements
- Type: pseudo-element
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 62+, Firefox Android 62+, Safari 1.1+, Safari iOS: not supported

## Example

Wrong:

```css
::selection {
  background: transparent;
}
```

Right:

```css
::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.

## Related cards

- [::highlight()](https://wonderium.dev/css/highlight.md)
- [::target-text](https://wonderium.dev/css/target-text.md)
- [user-select](https://wonderium.dev/css/user-select.md)
- [Pseudo-element](https://wonderium.dev/css/pseudo-element.md)
- [caret-color](https://wonderium.dev/css/caret-color.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/selection
- Last updated: 2026-11-25
