::selection
Quick Summary for ::selection
The ::selection CSS pseudo-element applies styles to the part of a document that has been highlighted by the user (such as clicking and dragging the mouse across text).
Code Usage for ::selection
::selection {   background-color: cyan; } 
More Details for ::selection

::selection

The ::selection CSS pseudo-element applies styles to the part of a document that has been highlighted by the user (such as clicking and dragging the mouse across text).

::selection {   background-color: cyan; } 

Allowable properties

Only certain CSS properties can be used with ::selection:

color background-color text-decoration and its associated properties text-shadow stroke-color, fill-color and stroke-width

In particular, background-image is ignored.

Syntax

/* Legacy Firefox syntax (version 61 and below) */ ::-moz-selection  ::selection 

Examples

HTML

This text has special styles when you highlight it. <p>Also try selecting text in this paragraph.</p> 

CSS

/* Make selected text gold on a red background */ ::selection {   color: gold;   background-color: red; }  /* Make selected text in a paragraph white on a blue background */ p::selection {   color: white;   background-color: blue; } 

Result

Accessibility concerns

Don't override selected text styles for purely aesthetic reasons — users can customize them to suit their needs. For people experiencing cognitive concerns or who are less technologically literate, unexpected changes to selection styles may hurt their understanding of the functionality.

If overridden, it is important to ensure that the contrast ratio between the text and background colors of the selection is high enough that people experiencing low vision conditions can read it.

Color contrast ratio is found by comparing the luminosity of the selected text and the selected text background colors. To meet current Web Content Accessibility Guidelines (WCAG), text content must have a contrast ratio of 4.5:1, or 3:1 for larger text such as headings. (WCAG defines large text as between 18.66px and 24px and bold, or 24px or larger.)

WebAIM: Color Contrast Checker MDN Understanding WCAG, Guideline 1.4 explanations Understanding Success Criterion 1.4.3 | W3C Understanding WCAG 2.0

Specifications

Specification
CSS Pseudo-Elements Module Level 4 # selectordef-selection

Note: ::selection was in drafts of CSS Selectors Level 3, but it was removed in the Candidate Recommendation phase because its was under-specified (especially with nested elements) and interoperability wasn't achieved (based on discussion in the W3C Style mailing list). It returned in Pseudo-Elements Level 4.

See also

pointer-events - control which events are active on the element Select your preferred language English (US)DeutschEspañolFrançais日本語Português (do Brasil)Русский中文 (简体) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote

minecraft
Random CSS Property

font-variant-alternates

The font-variant-alternates CSS property controls the usage of alternate glyphs. These alternate glyphs may be referenced by alternative names defined in @font-feature-values.
swash() css reference