{"id":181092,"date":"2014-08-29T19:36:53","date_gmt":"2014-08-30T02:36:53","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=181092"},"modified":"2022-02-14T07:49:04","modified_gmt":"2022-02-14T15:49:04","slug":"selection","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/selectors\/s\/selection\/","title":{"rendered":"::selection"},"content":{"rendered":"\n

Use your cursor select this sentence. Notice how as you select the text a background color fills the space? You can change the background color and color of selected text by styling ::selection<\/code>. Styling this pseudo element is great for matching user-selected text to your sites color scheme.<\/p>\n\n\n\n

p::selection {\n  background: #fff;\n  color: #ff0000;\n}<\/code><\/pre>\n\n\n\n\n\n\n\n
\"Screenshot<\/figure>\n\n\n\n

Take note that the double colon is necessary in the syntax for this pseudo element, despite that other pseudo-elements accept a single colon<\/a>.<\/p>\n\n\n\n

As seen above, you can style ::selection<\/code> on individual elements. You can also style the entire page by dropping the bare pseudo-element in your stylesheet.<\/p>\n\n\n\n

::selection { background: yellow; }<\/code><\/pre>\n\n\n\n

There are only three properties that ::selection<\/code> will work with:<\/p>\n\n\n\n

  • color<\/code><\/li>
  • background<\/code> (specifically the background-color<\/code>, background-image<\/code> longhand properties)<\/li>
  • text-shadow<\/code><\/li><\/ul>\n\n\n\n