Style Selected Text With CSS
CSS lets you change how highlighted text looks with the ::selection pseudo-element. It is a small detail, but matching the selection colours to a site’s palette can look rather nice.
::selection {
background: navy;
color: white;
}
That rule applies across the page. You can also limit it to a particular element:
article p::selection {
background: gold;
color: black;
}
Only a small set of properties applies to selected text, so keep the styling simple. Background and text colours are the useful pair.
Check their contrast as well. Selection is supposed to make text easier to identify; a subtle brand colour that hides the words rather misses the point.
Enjoyed reading this?
If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.
Related articles
- Making Links Easy to Recognize
Make links recognisable with underlines, clear focus states, useful text, and behaviour that does not surprise the reader.
- When CSS Layout Order Stops Matching the HTML
Keep Grid and Flexbox layouts aligned with the HTML order so keyboard focus and screen reader reading do not jump around the page.
- Keyboard Focus Styles With :focus-visible
Keep a clear keyboard focus indicator while avoiding the same focus ring after every pointer interaction.