Brand-aligned text selection styling using the ::selection pseudo-element
The default blue text selection highlight often clashes with custom brand palettes. In this studio, I'll show you how to customize the ::selection pseudo-element with on-brand background tints and contrasting typography for that extra layer of polish.
Three Steps to Custom Web Text Selection Highlights
Pick Palette & Contrast
Choose high-contrast brand background colors and matching text foregrounds, or select from curated presets like Electric Indigo and Cyber Neon.
Configure Text Shadows & URL Fragments
Add subtle luminous text-shadow glows and calibrate ::target-text styling for incoming Google text fragment search queries.
Export Cross-Browser CSS
Copy clean, standards-compliant CSS rules with vendor-prefixed fallbacks ready to paste into your global design system stylesheet.
Text Selection Pseudo-Element Comparison
| Pseudo-Element | Trigger Mechanism | Allowed CSS Properties | Browser Support |
|---|---|---|---|
| ::selection | User pointer drag or keyboard selection | color, background-color, text-shadow, text-decoration | All Modern Browsers (Chrome, Edge, Firefox, Safari) |
| ::target-text | URL scroll-to-text fragment navigation | color, background-color, text-decoration | Chrome, Edge, Safari 16.1+ |
| ::spelling-error | Browser spellcheck flagging | color, text-decoration, background-color | Chrome, Edge (W3C Highlight API) |
Features & CSS Best Practices
::selection Pseudo Styler
Native CSS pseudo-element styling with background colors, text tints, and glowing shadows.
WCAG 2.1 AA Compliance
Built-in contrast ratio checking ensures readable text highlights for assistive accessibility.
Target-Text URL Support
Styles Google search scroll-to-text fragments seamlessly alongside standard user selection.
Cross-Browser Engine
Includes -moz- selection vendor prefixes for legacy Gecko rendering engine backwards compatibility.
Frequently Asked Questions
Which CSS properties are allowed inside the ::selection pseudo-element?
The CSS specification restricts ::selection to visual text properties: color, background-color, cursor, caret-color, outline, text-decoration, and text-shadow. Layout properties like padding, margin, font-size, or transform are ignored for rendering performance.
How does ::target-text differ from ::selection?
::selection styles text highlighted by a user dragging their pointer or keyboard cursor. ::target-text styles the text directly navigated to by a browser URL text fragment (such as #:~:text=foo), enabling custom highlights when users land from search engine results.
How can I maintain WCAG accessibility compliance with custom selection colors?
Ensure that the foreground color and background color within ::selection achieve a minimum contrast ratio of 4.5:1 for standard body text (or 3:1 for large text). Avoid setting background-color without also explicitly specifying color, as user agent defaults may produce illegible combinations.
Can ::selection be scoped to specific cards or components?
Yes. While global selection applies via ::selection or html::selection, you can scope rules to any container using .card::selection, allowing different sections of a webpage (e.g. code blocks vs editorial prose) to have unique selection styling.