The ::selection pseudo-element lets you style text when a user highlights it with the mouse, keyboard, or touch. It is a small detail that makes your site feel polished and on-brand.
01
Highlight text
User selection.
02
color
Text color.
03
background
Highlight fill.
04
Scoped
p::selection.
05
Contrast
Readability.
06
Limited props
Only a few.
Fundamentals
Introduction
The CSS ::selection pseudo-element targets the portion of a document that a user has selected. When someone drags across a paragraph or uses Ctrl+A to select all text, your custom highlight colors appear instead of the browser’s default blue or gray.
This is a pseudo-element (note the double colon ::), similar to ::before and ::placeholder. It applies only to the selected text fragment, not the entire element.
Definition and Usage
Use ::selection to align highlighted text with your brand palette. Most developers set background-color and color for a clean, readable highlight. You can apply it globally or scope it to specific elements like code::selection or h1::selection.
💡
Beginner Tip
Try selecting the paragraph in the Live Preview below. The blue highlight is powered by ::selection — a few lines of CSS that replace the browser default.
Foundation
📝 Syntax
The syntax for the ::selection pseudo-element is:
syntax.css
::selection{/* CSS properties */}
You can also scope selection styles to a specific element:
Highlight this text to see a yellow selection with dark text.
How It Works
When the user selects any text on the page, the browser applies your background-color and color instead of its default. The ::-moz-selection rule is optional for older Firefox builds.
Example 2 — Brand-colored selection
Use your primary brand color as the highlight with white text for strong contrast.
A single global ::selection rule gives every selectable text fragment the same on-brand look. Store the colors in :root variables if you want one place to update them.
📄 Scoped & Special Cases
Apply different highlight colors to headings, paragraphs, and code.
Example 3 — Different selection per element
Give headings a green highlight and paragraphs a blue one using scoped selectors.
Scoped selectors override the global ::selection rule for matching elements. Headings get green; body copy gets blue. This is useful when different content types need distinct highlight treatments.
Example 4 — Code block selection
Style selection inside <code> blocks with a dark highlight suited for monospace snippets.
code::selection applies only when users highlight text inside inline code. The dark slate background makes copied snippets easy to see against the light code background.
Tips
💬 Usage Tips
Use contrasting colors — Light text on a dark highlight (or vice versa) keeps selected text readable.
Keep it subtle — A soft tint often feels better than a loud neon highlight on long articles.
Store in :root — Define --selection-bg and --selection-color as CSS variables for easy theming.
Scope when needed — Use code::selection or .article::selection for context-specific highlights.
Test on dark backgrounds — Default blue highlights can clash; pick colors that work on your page background.
Stick to allowed properties — color and background-color are the most reliable across browsers.
Watch Out
⚠️ Common Pitfalls
Unsupported properties — font-size, border, and padding do not apply to ::selection.
Low contrast — Pale yellow on white or light gray on white can make selected text hard to read.
user-select: none — Elements with this property cannot be selected; ::selection has no effect.
Images and icons — ::selection styles text, not images or SVG graphics.
Single colon typo — Use ::selection (pseudo-element), not :selection.
Overriding with !important — Rarely needed; a well-placed scoped rule is usually enough.
A11y
♿ Accessibility
Maintain contrast — Selected text should meet WCAG contrast guidelines against the highlight background.
Do not disable selection — Avoid user-select: none on body copy; users need to copy text.
Keyboard users — Selection styles apply to keyboard-driven highlights (Shift+arrows, Ctrl+A) as well as mouse selection.
High-contrast mode — Test that custom highlights remain visible when OS high-contrast settings are enabled.
Do not rely on color alone — Selection is a visual affordance; ensure content is readable without custom highlight colors.
🧠 How ::selection Works
1
User selects text
Mouse drag, double-click, or keyboard (Shift+arrows) highlights a text range.
Interaction
2
Browser creates selection
The highlighted fragment becomes a selectable pseudo-element target.
Render
3
::selection styles apply
Your background-color and color replace the browser default.
CSS
=
✎
Branded highlight appears
Only the selected characters are styled — the rest of the element keeps its normal appearance.
Compatibility
🖥 Browser Compatibility
The ::selection pseudo-element is supported in all modern browsers.
✓ Baseline · Modern browsers
Custom text highlights everywhere
::selection works in Chrome, Firefox, Safari, Edge, and Opera. Use background-color and color for the widest compatibility.
99%Global support
Google Chrome1+ · Desktop & Mobile
Full support
Mozilla Firefox62+ · ::selection standard
Full support
Apple Safari3.1+ · macOS & iOS
Full support
Microsoft Edge12+ / 79+ Chromium
Full support
Opera15+
Full support
::selection pseudo-element99% supported
Bottom line: Safe for all modern sites. Add ::-moz-selection only if you must support very old Firefox versions.
Wrap Up
🎉 Conclusion
The ::selection pseudo-element is a simple way to improve user experience by customizing how highlighted text looks. With just background-color and color, you can align text selection with your site’s design and make reading, copying, and learning feel more polished.
Keep contrast in mind, stick to supported properties, and avoid blocking text selection with user-select: none on content users need to copy.
Block text selection with user-select: none on articles
Expect ::selection to style images or icons
Forget mobile long-press selection behavior
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about ::selection
Use these points when styling highlighted text.
5
Core concepts
✎01
Highlighted text
User selection.
Purpose
::02
Pseudo-element
Double colon.
Syntax
🎨03
bg + color
Main props.
Style
p::04
Scoped rules
Per element.
Pattern
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
The ::selection pseudo-element targets the portion of text a user has highlighted with the mouse, keyboard, or touch. It lets you customize the background and text color of selected content instead of using the browser default.
Only a limited set applies: mainly color, background-color, and text-shadow. Properties like font-size, border, margin, and padding are ignored on ::selection.
Yes. Use scoped selectors such as h1::selection and p::selection to give different highlight colors to different elements.
If an element or its parent has user-select: none, the text cannot be selected and ::selection will have no visible effect.
Yes. ::selection is supported in all modern browsers. Older Firefox versions used ::-moz-selection, but current Firefox supports the standard syntax.