CSS ::selection Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Text Highlighting

What You’ll Learn

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.

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.

📝 Syntax

The syntax for the ::selection pseudo-element is:

syntax.css
::selection {
  /* CSS properties */
}

You can also scope selection styles to a specific element:

scoped-selection.css
p::selection {
  background-color: #2563eb;
  color: #ffffff;
}

Supported Properties

Only a limited set of CSS properties applies to ::selection:

color background-color text-shadow -webkit-text-stroke text-decoration

Note: Properties like font-size, border, margin, and padding have no effect on selected text.

::selection { } p::selection ::-moz-selection user-select

Syntax Rules

  • Use the double-colon form ::selection (pseudo-element, not pseudo-class).
  • Pair background-color with color for readable highlights.
  • Scope with element selectors: h1::selection, code::selection.
  • Add ::-moz-selection only if you need legacy Firefox support.
  • Text with user-select: none cannot be highlighted.

Related Topics

  • ::placeholder — another text-focused pseudo-element
  • color — sets selected text color
  • background-color — sets highlight fill
  • user-select — controls whether text can be selected
  • :root — store selection colors as CSS variables

⚡ Quick Reference

QuestionAnswer
Selector typePseudo-element
What it targetsUser-highlighted text
Most used propertiesbackground-color, color
Common pattern::selection { background: #2563eb; color: #fff; }
Scoped examplecode::selection { background: #1e293b; color: #f8fafc; }
Browser supportAll modern browsers

When to Use ::selection

::selection is ideal for polishing text-heavy pages:

  • Blog posts and articles — Match highlight colors to your brand palette.
  • Documentation sites — Make code snippets stand out when copied.
  • Landing pages — Small UX detail that signals attention to design.
  • Dark themes — Use a softer highlight that works on dark backgrounds.
  • Educational content — Help learners see what they have selected clearly.

👀 Live Preview

Select any part of the text below to see custom ::selection styling in action:

Highlight this paragraph with your mouse or keyboard. The selected text uses a blue background and white foreground instead of the browser default.

Tip: On mobile, long-press and drag to select text.

Examples Gallery

Practice ::selection with basic highlights, brand colors, scoped element styles, and code-block selection.

📜 Core Patterns

Style highlighted text with background-color and color.

Example 1 — Basic ::selection styling

Replace the default browser highlight with a yellow background and dark text.

selection-basic.css
::selection {
  background-color: #fef08a;
  color: #1e293b;
}

::-moz-selection {
  background-color: #fef08a;
  color: #1e293b;
}
Try It Yourself

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.

selection-brand.css
::selection {
  background-color: #2563eb;
  color: #ffffff;
}
Try It Yourself

How It Works

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.

selection-scoped.css
h1::selection {
  background-color: #16a34a;
  color: #ffffff;
}

p::selection {
  background-color: #2563eb;
  color: #ffffff;
}
Try It Yourself

How It Works

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.

selection-code.css
code::selection {
  background-color: #1e293b;
  color: #f8fafc;
}

code {
  font-family: ui-monospace, monospace;
  background: #f1f5f9;
  padding: 0.15rem 0.35rem;
  border-radius: 0.25rem;
}
Try It Yourself

How It Works

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.

💬 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 propertiescolor and background-color are the most reliable across browsers.

⚠️ Common Pitfalls

  • Unsupported propertiesfont-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.

♿ 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.

🖥 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 Chrome 1+ · Desktop & Mobile
Full support
Mozilla Firefox 62+ · ::selection standard
Full support
Apple Safari 3.1+ · macOS & iOS
Full support
Microsoft Edge 12+ / 79+ Chromium
Full support
Opera 15+
Full support
::selection pseudo-element 99% supported

Bottom line: Safe for all modern sites. Add ::-moz-selection only if you must support very old Firefox versions.

🎉 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.

💡 Best Practices

✅ Do

  • Use high-contrast highlight and text colors
  • Store selection colors in :root CSS variables
  • Scope styles for code blocks with code::selection
  • Test keyboard selection (Shift+arrows, Ctrl+A)
  • Keep highlights subtle on long-form content

❌ Don’t

  • Apply font-size or border to ::selection
  • Use low-contrast highlight colors
  • Block text selection with user-select: none on articles
  • Expect ::selection to style images or icons
  • Forget mobile long-press selection behavior

Key Takeaways

Knowledge Unlocked

Five things to remember about ::selection

Use these points when styling highlighted text.

5
Core concepts
:: 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.

Practice in the Live Editor

Open the HTML editor and experiment with ::selection, brand highlights, and scoped text selection styles.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful