CKTIS / CSS
::selection — styling selected content
The ::selection pseudo-element styles the selected portion of a document. It controls selection colors without changing content or page layout.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
::selection { color: <color>; background-color: <color>; }
selector::selection { color: <color>; background-color: <color>; }::selection represents the portion of a document selected as the target of a browser operation, such as copying. It does not create a selection: it only controls its appearance. Writing p::selection limits the rule to selected content associated with p elements.
Only a limited set of properties that do not change layout applies. The basic choices are color and background-color; set them together and maintain readable contrast. Properties such as padding and font-size will not change the selection’s appearance.
Highlight inheritance is special: unspecified supported properties can come from the parent’s corresponding pseudo-element. In the example, strong::selection inherits the background and changes only the text color. Use :root::selection for a document-wide default; bare ::selection matches every element and can override inherited highlight settings.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Selection styling</title>
<style>
:root::selection {
color: #ffffff;
background-color: #003366;
}
strong::selection {
color: #ffff00;
}
</style>
<p>Select this <strong>important text</strong>.</p>
</html>Result
When the whole sentence is selected, its selection background is #003366 throughout. Selected text is #ffffff except for “important text”, which is #ffff00. Unselected text is unaffected.
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.