How to Style Custom Text Selection in CSS: Colors, Gradients & Highlights
To style text selection in CSS, use the ::selection pseudo-element. Browsers strictly restrict the properties that can be styled inside ::selection to: color, background-color, text-decoration, text-shadow, and stroke/fill. Always ensure selected text maintains a minimum 4.5:1 WCAG contrast ratio between the foreground text and the highlight background color.
Highlight any part of this text to see the custom branded selection in action!
Step 1: Global vs Scoped ::selection Pseudo-Elements
You can apply text selection highlights globally across the entire website on ::selection, or scope unique highlight styles to specific sections (e.g. dark indigo in body copy, vibrant cyan inside code blocks).
/* Global Brand Selection Highlight */
::selection {
background-color: #6366f1;
color: #ffffff;
}
/* Scoped Code Block Selection Highlight */
pre code::selection {
background-color: #38bdf8;
color: #0f172a;
}
Step 2: Allowed Properties in ::selection: What Works & What Fails
For browser security and rendering performance, the CSS specification strictly limits what properties can be declared inside ::selection. Properties like font-size, transform, padding, and border are completely ignored.
::selection {
/* ALLOWED: */
color: #ffffff;
background-color: #ec4899;
text-decoration: underline wavy #fff;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
/* IGNORED (Do not use): */
font-size: 18px; /* Ignored */
margin: 10px; /* Ignored */
padding: 4px; /* Ignored */
}
Step 3: Guaranteeing WCAG Contrast on Selected Text
A frequent bug occurs when developers set a light background on ::selection without changing color. On dark mode websites with white text, selecting text makes the white text invisible against a pastel highlight. Always declare both background-color AND color together.
/* SAFE: Both foreground and background are explicitly paired */
::selection {
background-color: #a78bfa; /* Light Purple Highlight */
color: #110e1f; /* Dark Navy Text (11.8:1 Contrast) */
}
Step 4: Semi-Transparent Alpha Highlights with color-mix()
Setting an opaque selection background completely hides underlying image backgrounds. By using color-mix() or hex alpha, selection feels integrated and luminous.
::selection {
background-color: color-mix(in srgb, var(--accent) 45%, transparent);
color: #ffffff;
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Which CSS properties are allowed inside the ::selection pseudo-element?
The only allowed properties inside ::selection are: color, background-color, text-decoration, text-shadow, and SVG fill and stroke.
Why does setting padding or border inside ::selection have no effect?
Allowing geometry properties like padding, margin, or font size inside text selection would force browser layout reflows every time a user drags their mouse across text, causing severe performance jank.
Why should I always specify color when setting ::selection background-color?
If you only declare background-color, the text keeps its original color. If that text color matches or clashes with your highlight color, selected text becomes completely unreadable.
Can I apply different selection colors to different parts of the same page?
Yes. ::selection can be scoped to any parent selector, such as .hero ::selection or aside ::selection.
Does ::selection work with vendor prefixes like -moz-selection?
Modern browsers have supported standard unprefixed ::selection for many years. Vendor prefixes like ::-moz-selection are no longer required.