::selection
::selection lets you style the portion of text a user is currently highlighting with their cursor (or has selected via keyboard, e.g. Ctrl/Cmd+A). Browsers apply a default selection color — usually a shade of blue — but you can override it to match your site's branding.
Basic usage
::selection {
background-color: #2563eb;
color: white;
}Applied to the universal selector like this, every bit of selectable text on the page picks up the new highlight color when a user drags to select it.
Scoping it to part of the page
/* Only override selection color inside article content,
leaving code blocks or other areas with the default */
.article ::selection {
background-color: #fde68a;
color: #1f2937;
}Worked example: branded selection color
:root {
--brand-color: #7c3aed;
}
::selection {
background-color: var(--brand-color);
color: white;
}
/* Give code samples a slightly different, high-contrast
selection color so selected code stays legible */
pre ::selection,
code ::selection {
background-color: #fbbf24;
color: #1f2937;
}A subtle, on-brand selection color is a small but noticeable polish detail — especially on marketing and documentation pages where users often copy text.
Make sure the chosen
color/background-colorpair still has enough contrast to remain readable while selected.Because so few properties apply, resist the urge to add elaborate styling here — stick to color and text-decoration adjustments.